Disabling a Multibranch Pipeline using Job DSL
Using Job DSL to manage your pipelines is useful, but sometimes you want to not run a pipeline, for instance so it doesn't trigger in your test environment.
Post Jenkins 2.263
It appears that between Jenkins 2.235.5 (when I last checked this) and Jenkins 2.263, this is now possible to do via a configure
block:
factory.multibranchPipelineJob(jobLocation) {
configure {
it / disabled << 'true'
}
}
Pre Jenkins 2.263
In the Jenkins UI, you can click a Disable
button which disables the project, but interestingly this doesn't change the job's XML behind the scenes, so it seems to be controlled through some other Jenkins state.
This means we need to think a bit out of the box to do this, which led me to the use of the excludes
property, where we can exclude all the branches that are discovered:
factory.multibranchPipelineJob(jobLocation) {
branchSources {
/* for example */
git {
// other config
excludes('*')
}
/* for example, when using GitHub */
github {
// other config
excludes('*')
}
I'd also recommend, in the cases we disable the pipeline, we update the description of the job to make it clear it's disabled.