I\'m trying to automatically trigger \'Branch Indexing\' on a Multibranch Pipelines job in Jenkins.
At the moment, only one method seems to actually work, which is p
Based on @jjc's answer, I've created a version using the build step also for triggering the scan:
String downStreamProject = 'my-folder/my-multibranch-project'
String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}"
if (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
// we would need "wait: true", which is not possible as of now
// https://github.com/jenkinsci/pipeline-build-step-plugin/blob/3ff14391fe27c8ee9ccea9ba1977131fe3b26dbe/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepExecution.java#L66
build job: downStreamProject, wait: false
// continue only once the expected job appears
while (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
sleep(1)
}
}
build downStreamJob
This requires the following signatures to be approved:
method jenkins.model.Jenkins getItemByFullName java.lang.String
staticMethod jenkins.model.Jenkins getInstance
The method ComputedFolder.scheduleBuild()
can be invoked from a groovy script.
I have just triggered branch indexing in one multibranch pipeline project from the groovy code in a different multibranch pipeline project, which is then triggering a downstream build in that project.
The code is something like:
@NonCPS
void scanRepo(String downStreamProjectName) {
Jenkins.instance.getItemByFullName(downStreamProjectName).scheduleBuild()
}
...
String downStreamProject = 'my-folder/my-multibranch-project'
String downStreamJob = "${downStreamProject}/${env.BRANCH_NAME}"
if (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
scanRepo(downStreamProject)
while (Jenkins.instance.getItemByFullName(downStreamJob) == null) {
sleep(1)
}
}
build([job: downStreamJob, wait: false, quietPeriod: 0])
Notice that Jenkins.instance.getItemByFullName(downStreamProjectName)
is the WorkflowMultiBranchProject
which is not Serializable
, so some care needs to be taken.
The easiest option by far (that I'm aware of) is to remotely tell the Jenkins Git plugin that there's a new commit for a defined repository. However, this will not trigger Jenkins to start a job immediately. What happens is that the Git plugin starts (re-)indexing the specific repository. The Jenkins job is then started if changes are detected.
From your repository (GitHub, GitLab, etc.) you should trigger the following URL:
http://my-jenkins-host/git/notifyCommit?url=git@gitlab.example.com:group/repository.git&delay=0sec
The value for url
must match the SCM URL you configured in the Jenkins job (Git plugin)!
Gotcha: it may be that your Jenkins is not deployed under the root context (/
) in which case the URL would be http://my-jenkins-host/context-path/git/...