Issue
I have a jenkins job (A
) that creates and pushes a new git branch with the pattern releases/major.minor
. I have jenkins multibranch pipeline (B
) that builds for all branches named releases/*
. Immediately after A
completes, I want to trigger B
on the newly-created branch, but jenkins won't run B/major.minor
until there's a new scan.
How do I trigger a scan?
Solution
You can scan multibranch projects with the build
step. However, you must include wait: false
or else you'll get the following error:
ERROR: Waiting for non-job items is not supported
Unfortunately, that means if you want to run the multibranch pipeline on the associated branch, you need to manually check for the existence of the job.
def ensureMultibranchJobExists(Map args) {
def branch = args['branch']?.replaceAll('/', '%252F')
def rootJob = args['rootJob']
if (branch == null) {
throw new NullPointerException('branch is required')
}
if (rootJob == null) {
throw new NullPointerException('rootJob is required')
}
// env.JENKINS_URL ends with a slash.
env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL = "${env.JENKINS_URL}job/$rootJob/job/$branch/"
print "Ensuring multibranch job exists: ${env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL}"
def lastHttpStatusCode = null
for (int i=0; i < 12; i++) {
lastHttpStatusCode = sh(
returnStdout: true,
script: '''
#!/bin/bash
set -euo pipefail
curl \
--output /dev/null \
--silent \
--user devtools:<MY_TOKEN> \
--write-out '%{http_code}' \
"${ENSURE_MULTIBRANCH_JOB_EXISTS_URL}" \
;
'''.trim(),
)
if (lastHttpStatusCode == '200') {
break
} else {
print "Last status code: $lastHttpStatusCode"
}
sleep(
time: 10,
unit: 'SECONDS',
)
}
if (lastHttpStatusCode != '200') {
error "${env.ENSURE_MULTIBRANCH_JOB_EXISTS_URL} failed. Expected 200 status code, but received: $lastHttpStatusCode"
}
}
Answered By - Heath Borders