How to disable branch in Multibranch pipeline with Jenkinsfile

Gayan Viranka picture Gayan Viranka · Mar 14, 2018 · Viewed 11.9k times · Source

I have a Multi branch pipeline project in Jenkins. I want to disable a branch while it is listed in pipeline project. I can add an exception to surpass scm triggering. But I want to disable all the triggering including manual triggering. If I used "Disable this project" under "Build triggers" in job created for a branch, that option is not selected when I reload the page(There are no save/apply buttons which is available for single branch pipelines). It only keeps follwing configuration that was configured in Jenkinsfile.

pipelineTriggers([
    snapshotDependencies(),
]),

Are there any way to specify "Disable this project" in Jenkinsfile

Answer

Here_2_learn picture Here_2_learn · Mar 14, 2018

True, you cannot disable the project from Jenkins Job as multibranch pipeline doesn't give control to change the settings from the job.

Two ways to stop your branch from building.

1) Disable the Branch

To get control of the settings, go to PROJECT(project you want to modify the settings) > Configure > Projects( enable advanced settings)

enter image description here

Enter the branch in "Exclude branch" and save the settings.

2) Rename Jenkinsfile

If you don't have control of the project settings, the easy way is to rename the Jenkinsfile in the project/branch.

enter image description here

This configuration will define to trigger a build if a branch/project has "Jenkinsfile" in it. Hence, renaming it will not trigger a build.

Hope this helps..

UPDATE : AFAIK, you can't disable the project from Jenkisfile, but as workaround you can configure the cronjob as follows:

properties([pipelineTriggers([cron('')])])

If configuration is not available in cron, the build will not trigger at all.