How to specify when branch NOT (branch name) in jenkinsfile? How to specify when branch NOT (branch name) in jenkinsfile? jenkins jenkins

How to specify when branch NOT (branch name) in jenkinsfile?


With this issue resolved, you can now do this:

stage('Example (Not master)') {   when {       not {           branch 'master'       }   }   steps {     sh 'do-non-master.sh'   }}


You can also specify multiple conditions (in this case branch names) using anyOf:

stage('Example (Not master nor staging)') {   when {       not {          anyOf {            branch 'master';            branch 'staging'          }       }   }   steps {     sh 'do-non-master-nor-staging.sh'   }}

In this case do-non-master-nor-staging.sh will run on all branches except on master and staging.

You can read about built-in conditions and general pipeline syntax here.


The link from your post shows an example with the scripted pipeline syntax. Your code uses the declarative pipeline syntax. To use the scripted pipeline within declarative you can use the script step.

stage('Example') {    steps {        script {             if (env.BRANCH_NAME != 'master' && env.BRANCH_NAME != 'staging') {                echo 'This is not master or staging'            } else {                echo 'things and stuff'            }        }    }}