How to disable branch in Multibranch pipeline with Jenkinsfile
Asked Answered
S

2

11

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

Sinking answered 14/3, 2018 at 5:17 Comment(0)
B
9

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.

Breechcloth answered 14/3, 2018 at 6:5 Comment(1)
Thanks for the answer @Here_2_learn. Is there any way to specify disable option in Jenkinsfile just like I did with snapshotDependencies()?Sinking
B
3

You can do something like this:

    stages {
    stage('Build') {
        when { 
            not { 
                branch 'master' 
            }
        }
        steps {
            ...
        }
    }
}

Which isn't quite the same as not running the pipeline at all, but at least, it runs very quickly, and doesn't actually do the build, which is useful to have it not fail - say, if you are using a maven / gitflow pattern, where the master branch should not be built more than once, since the deployment would fail...

Bywoods answered 20/8, 2018 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.