Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable branch in Multibranch pipeline with Jenkinsfile

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

like image 601
Gayan Viranka Avatar asked Mar 14 '18 05:03

Gayan Viranka


2 Answers

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.

like image 189
Here_2_learn Avatar answered Sep 24 '22 10:09

Here_2_learn


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...

like image 38
user2163960 Avatar answered Sep 23 '22 10:09

user2163960