Limit the branches in a multibranch pipeline project

Here was a question that I was searching for… I think it’s solved now.

With Multibranch Pipeline, how do you restrict or limit the branches that will be processed?

Option 1:
Within the Jenkinfile code, in the pipeline, this example will limit branches to develop, master, and any pull request. It only affects a given stage. The rest of the pipeline will still run on all branches.

stage('My Stage 1') {
        when {
            anyOf{
                branch 'develop'
                branch 'master'
                expression { env.CHANGE_ID != null }
                }
           } 
            steps {
                sh '''#!/bin/bash
                set -xe
                echo "hello world"
                '''

Option 2:

In the Jenkins UI

Dashboard → job → Configuration → Branch Sources → (add this option) “Filter by name (with regular expression)”

Set a value such as this example: (develop|master|PR-.*)

That will limit branches to develop, master, and any pull request.

If anyone knows variations on the theme, alternative syntaxes, please post them.

Could the regex filter be written this way instead: develop master PR-.* , as a space separated string?

1 Like