From bd85abd2356509821cd214045c080ac1eb09e17e Mon Sep 17 00:00:00 2001 From: syedimranhassan Date: Tue, 26 Mar 2024 14:44:55 +0500 Subject: [PATCH] chore: Re-add experiment job resource used by Optimizely job --- experiments/jobs/BackUpOptimizely.groovy | 80 +++++++++++++++++++++ experiments/resources/back_up_optimizely.sh | 41 +++++++++++ 2 files changed, 121 insertions(+) create mode 100644 experiments/jobs/BackUpOptimizely.groovy create mode 100644 experiments/resources/back_up_optimizely.sh diff --git a/experiments/jobs/BackUpOptimizely.groovy b/experiments/jobs/BackUpOptimizely.groovy new file mode 100644 index 000000000..fbf4ddaf6 --- /dev/null +++ b/experiments/jobs/BackUpOptimizely.groovy @@ -0,0 +1,80 @@ +/* +Pulls current state of experiment code in Optimizely and backs up to git history branch in edx/optimizely-experiments. + +Vars consumed for this job: + * FOLDER_NAME: folder + * NOTIFY_ON_FAILURE: alert@example.com + * OPTIMIZELY_TOKEN: optimizely api token (required) + * SECURE_GIT_CREDENTIALS: secure-bot-user (required) + * SSH_AGENT_KEY : ssh-credential-name +*/ +package experiments.jobs + +class BackUpOptimizely { + + public static def job = { dslFactory, extraVars -> + + dslFactory.job(extraVars.get("FOLDER_NAME","Experiments") + "/back-up-optimizely") { + + environmentVariables { + env('GIT_AUTHOR_NAME', extraVars.get('GIT_AUTHOR_NAME')) + env('GIT_AUTHOR_EMAIL', extraVars.get('GIT_AUTHOR_EMAIL')) + env('GIT_COMMITTER_NAME', extraVars.get('GIT_AUTHOR_NAME')) + env('GIT_COMMITTER_EMAIL', extraVars.get('GIT_AUTHOR_EMAIL')) + } + + def gitCredentialId = extraVars.get('SECURE_GIT_CREDENTIALS','') + + wrappers { + credentialsBinding { + string('OPTIMIZELY_TOKEN', 'optimizely-token') + } + sshAgent(gitCredentialId) + } + + multiscm { + git { + remote { + url(extraVars.get('OPTIMIZELY_EXPERIMENTS_REPO')) + branch("history") + if (gitCredentialId) { + credentials(gitCredentialId) + } + } + extensions { + cleanAfterCheckout() + pruneBranches() + relativeTargetDirectory('optimizely-experiments') + } + } + git { + remote { + url("https://github.com/edx/py-opt-cli.git") + branch('master') + } + extensions { + cleanAfterCheckout() + pruneBranches() + relativeTargetDirectory('py-opt-cli') + } + } + } + + + triggers { + // Trigger every hour + cron("H * * * *") + } + + steps { + shell(dslFactory.readFileFromWorkspace("experiments/resources/back_up_optimizely.sh")) + } + + // Alert on call revenue engineer on failure. + publishers { + mailer(extraVars.get('NOTIFY_ON_FAILURE'), false, false) + } + } + } + +} diff --git a/experiments/resources/back_up_optimizely.sh b/experiments/resources/back_up_optimizely.sh new file mode 100644 index 000000000..f0f7715a6 --- /dev/null +++ b/experiments/resources/back_up_optimizely.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -exuo pipefail + +set +u +. /edx/var/jenkins/jobvenvs/virtualenv_tools.sh +# creates a venv with its location stored in variable "venvpath" +create_virtualenv --python=python3.6 --clear +. "$venvpath/bin/activate" +set -u + +# Required by click http://click.pocoo.org/5/python3/ +export LC_ALL=C.UTF-8 +export LANG=C.UTF-8 +export GIT_AUTHOR_NAME +export GIT_AUTHOR_EMAIL +export GIT_COMMITTER_NAME +export GIT_COMMITTER_EMAIL + + +cd $WORKSPACE/py-opt-cli +pip install -r requirements.txt +pip install -e . +env + +cd $WORKSPACE/optimizely-experiments + +# Retry 5 times before giving up on optimizely +for i in {1..5} +do + # Don't sleep the first time through the loop + [ $i -gt 1 ] && sleep 300 + + # Save the return-code of the pull command + py-opt-cli pull && s=0 && break || s=$? +done +# Fail the build with the return code of the pull command if it failed after 5 retries +(exit $s) + +git add -A +git diff --cached --quiet || git commit -am "history commit job # ${BUILD_ID}" +git push origin HEAD:history