diff options
author | Matt Rendina <mrendina@stsci.edu> | 2017-10-12 13:54:19 -0400 |
---|---|---|
committer | Matt Rendina <mrendina@stsci.edu> | 2017-10-12 14:56:40 -0400 |
commit | aee0a35f390ce7fff59c459c971bd5e564224de4 (patch) | |
tree | 8b647a67e3183ed844fbecfe56aca43219870faa | |
parent | c735578a31384f384ee55a7fdfdd3168fdc53b83 (diff) | |
download | build_control-aee0a35f390ce7fff59c459c971bd5e564224de4.tar.gz |
Add generalized scheduled_trigger pipeline script
-rw-r--r-- | jenkins/scheduled_trigger.groovy | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/jenkins/scheduled_trigger.groovy b/jenkins/scheduled_trigger.groovy new file mode 100644 index 0000000..e8a3bdd --- /dev/null +++ b/jenkins/scheduled_trigger.groovy @@ -0,0 +1,72 @@ +// Triggers the execution of one or more jobs found within 'abs_jobs_folder'. +// +// This job may be scheduled to run at particular intervals to schedule +// collections of jobs simultaneously. +// +// Parameters to be passed to the triggered jobs are defined in the job +// configuration interface within Jenkins. +// PLATFORMS +// abs_jobs_folder +// +// Parameter names that are prefixed by the text appearing as the value of +// 'this.job_param_id' will be passed along as run parameters to each job +// that this job triggers. + +this.job_param_id = "(downstream)" + +node("master") { + + // Print version info + build_control_repo = scm.getUserRemoteConfigs()[0].getUrl() + build_control_bt_spec = scm.branches[0].toString() + if (build_control_bt_spec.find("tags") != null) { + build_control_branch = "master" + build_control_tag = build_control_bt_spec.tokenize("/")[-1] + } else { // a branch, including */master + build_control_branch = build_control_bt_spec.tokenize("/")[-1] + } + println("Build control repo: ${build_control_repo}") + println("Build control branch: ${build_control_branch}") + + // From Credentials Binding plugin: + withCredentials([usernamePassword(credentialsId: 'ScopedJenkinsLocal', + usernameVariable: 'USERNAME', + passwordVariable: 'PASSWORD')]) { + + println(params) + // Collect the parameters to pass along to triggered jobs (as opposed + // to parameters used to control the behavior of this job). Compose + // the URL string used to pass those parameter values to the jobs + // being triggered via the REST API. + params_url = "?" + params.each { + key = it.key.toString() + val = it.value.toString() + if (key.find(this.job_param_id) != null) { + println("${key}, ${val}") + index = this.job_param_id.size() + param_name = key[index..-1].trim() + println(param_name) + params_url = "${params_url}${param_name}=${val}" + } + } + + // Obtain authentication "crumb" for this session. + url_base = env.JENKINS_URL.split("://")[1] + crumb_url_path = "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)" + crumb_url = "http://${USERNAME}:${PASSWORD}@${url_base}/${crumb_url_path}" + CRUMB = sh (script: "curl -s '${crumb_url}'", returnStdout: true).trim() + println("CRUMB: ${CRUMB}") + + // Trigger all requested jobs with supplied parameter(s). + println("Platforms:\n${PLATFORMS}") + for (platform in PLATFORMS.tokenize()) { + println("Triggering _dispatch job for ${platform}...") + trigger_url = "http://${url_base}/job/${abs_jobs_folder}/job/${platform}/" + + "job/_dispatch/buildWithParameters${params_url} " + + "-u ${USERNAME}:${PASSWORD}" + println(trigger_url) + sh (script: "curl -s -S -X POST -H ${CRUMB} ${trigger_url}") + } + } +} |