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

Solution 1:

With this issue resolved, you can now do this:

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

Solution 2:

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.