<p>Instead of configuring it in the UI (as perfectly described by <a href="https://devops.stackexchange.com/a/3405/">casey vega's answer</a>, you can also do this within your Jenkinsfile:</p>
<h2>Declarative Pipeline</h2>
<p>Add the following condition within any stage directive:</p>
<pre><code>when { anyOf { branch 'master'; branch 'release/**' } }
</code></pre>
<p>As you can see, it's possible to use exact branch names as well as some wildcards (ANT style path glob pattern). It's also possible to use regex and some more fancy conditions and combinations. Please have a look at Jenkins' offical docs on the <code>when</code> directive for reference and detailed information.</p>
<h2>Scripted Pipeline</h2>
<p>Just enclose the part you want to have run conditionally in the following code:</p>
<pre><code>if (env.BRANCH_NAME == 'master' || (env.BRANCH_NAME).startsWith('release/')) {
  // Your Pipeline goes here
}
</code></pre>
<p>This can be used almost everywhere within your (scripted) Jenkinsfile. As you can see, it's just some simple string comparison. For more complex Regex comparisons, take a look at Groovy's <a href="https://groovy-lang.org/operators.html#_find_operator" rel="nofollow noreferrer">Find and Match operators</a>, which also work fine here.</p>
<h2>Conclusion</h2>
<p>I would prefer using this method (although <a href="https://devops.stackexchange.com/questions/3400/how-to-disable-some-branches-in-multibranch-jenkins-builds/3405?noredirect=1#comment12544_3405">I recommended it otherwise some time ago</a>), as you can then check-in your pipeline configuration into your source control and you're more on the Configuration-as-Code approach as when you configure it in your Jenkins UI.</p>

CC BY-SA 4.0
2022-10-05T21:48:40.653Z
2022-10-05T21:48:40.653Z
biolauri
<p>Simply delete the Jenkinsfile on the branch(es) you don't want to have corresponding branch jobs for. This will delete the branch job (of course, iff you have set your "Orphaned Item Strategy" appropriately). </p>

<p>From the perspective of a Jenkins Multibranch Pipeline Project, this has the same effect as deleting the branch. This is because it is simply scanning for branches that contain Jenkinsfiles as its criterion for when to create (or delete) a corresponding branch job.</p>

<p>Revert the commit to restore the Jenkinsfile if/when you need the branch job again.</p>

CC BY-SA 4.0
2020-04-24T16:05:13.577Z
This is too much work. When you create a branch and delete a file you're altering GIT history for no good reason. It's much better to filter at the Jenkins level so you don't have to do anything.
casey vega
CC BY-SA 4.0
2020-06-01T20:49:30.167Z
@caseyvega you don't think enabling and disabling CI is worthy of a commit in history? This seems like the best answer to me.
Adam Smith
CC BY-SA 4.0
2020-06-14T18:40:46.030Z
OP is fairly clear why he doesn't want to do that. Nothing prevents you from managing Jenkins jobs and this setting via GitOps/JCasC. Secondly, what If I want to create a branch that Jenkins doesn't automatically pick up? Couple this with deleted files and messy pull requests/merges. This solution isn't wrong, it just doesn't scale or meet OP's requirements.
casey vega
CC BY-SA 4.0
2020-02-25T19:59:28.807Z
2020-02-25T20:39:15.597Z
2020-02-25T20:39:15.597Z
timblaktu
<p>Jenkins can filter branches in a multibranch pipeline by name using a wildcard or regular expression.</p>

<p><a href="https://i.sstatic.net/P2TRv.png" rel="noreferrer"><img src="https://i.sstatic.net/P2TRv.png" alt="jenkins filter branches"></a></p>

CC BY-SA 4.0
2020-02-18T22:42:13.257Z
Any way to do this within a Jenkinsfile (so it's reproducible)?
xjcl
CC BY-SA 4.0
2020-02-26T12:45:38.100Z
Yes, you can add `when { anyOf { branch 'master'; branch 'release/**' } }` within any stage (in declarative) or `if (env.BRANCH_NAME == 'master' || (env.BRANCH_NAME).startsWith('release/')) {}` within scripted pipeline (almost anywhere). For the first option, there's more available in https://jenkins.io/doc/book/pipeline/syntax/#when.  
Anyway, the best way is to configure this in the multibranch pipeline plugin, as stated in the answer.
biolauri
CC BY-SA 4.0
2022-10-02T12:00:56.047Z
@biolauri You comment would be worth its own answer.
not2savvy
CC BY-SA 4.0
2022-10-05T21:50:14.183Z
@not2savvy: I did: https://devops.stackexchange.com/a/16690 and also updated my recommendation. Feel free to take a look and leave a comment if there's anything I can improve. :-)
biolauri
CC BY-SA 3.0
2018-02-21T20:07:27.517Z
2018-02-21T20:07:27.517Z
casey vega
<p>I have a multibranch job set to run any branch with a Jenkinsfile.</p>

<p>I have some options I can think of if I want to remove a branch from the list of jobs running for the multi-branch pipeline.</p>

<ol>
<li>I can delete the branch</li>
<li>I can delete the Jenkinsfile in that branch</li>
</ol>

<p>The second solution is good, except I need to commit and push that to the git repo for my branch, and if that branch is merged into another branch it blows away the Jenkinsfile.</p>

<p>What is the best way to disable only some branches of a multibranch pipeline?</p>

<jenkins-pipeline>
How to disable some branches in Multibranch Jenkins Builds?
David West
# How to disable some branches in Multibranch Jenkins Builds?

(Asked by: username_0 on 2018-02-21)

I have a multibranch job set to run any branch with a Jenkinsfile.

I have some options I can think of if I want to remove a branch from the list of jobs running for the multi-branch pipeline.

1. I can delete the branch
2. I can delete the Jenkinsfile in that branch

The second solution is good, except I need to commit and push that to the git repo for my branch, and if that branch is merged into another branch it blows away the Jenkinsfile.

What is the best way to disable only some branches of a multibranch pipeline?

**Tags:** jenkins-pipeline


---

## 3 answers

### Answer by username_3 on 2018-02-21. Score: 30 | Accepted answer

Jenkins can filter branches in a multibranch pipeline by name using a wildcard or regular expression.

[![jenkins filter branches](https://i.sstatic.net/P2TRv.png)](https://i.sstatic.net/P2TRv.png)

> **Comments:**
> **username_5**: Any way to do this within a Jenkinsfile (so it's reproducible)?  
> **username_1**: Yes, you can add `when { anyOf { branch 'master'; branch 'release/**' } }` within any stage (in declarative) or `if (env.BRANCH_NAME == 'master' || (env.BRANCH_NAME).startsWith('release/')) {}` within scripted pipeline (almost anywhere). For the first option, there's more available in https://jenkins.io/doc/book/pipeline/syntax/#when.   Anyway, the best way is to configure this in the multibranch pipeline plugin, as stated in the answer.  
> **username_6**: @username_1 You comment would be worth its own answer.  
> **username_1**: @username_6: I did: https://devops.stackexchange.com/a/16690 and also updated my recommendation. Feel free to take a look and leave a comment if there's anything I can improve. :-)  

---

### Answer by username_1 on 2022-10-05. Score: 2

Instead of configuring it in the UI (as perfectly described by username_3's answer, you can also do this within your Jenkinsfile:
Declarative Pipeline
Add the following condition within any stage directive:
when { anyOf { branch 'master'; branch 'release/**' } }

As you can see, it's possible to use exact branch names as well as some wildcards (ANT style path glob pattern). It's also possible to use regex and some more fancy conditions and combinations. Please have a look at Jenkins' offical docs on the when directive for reference and detailed information.
Scripted Pipeline
Just enclose the part you want to have run conditionally in the following code:
if (env.BRANCH_NAME == 'master' || (env.BRANCH_NAME).startsWith('release/')) {
  // Your Pipeline goes here
}

This can be used almost everywhere within your (scripted) Jenkinsfile. As you can see, it's just some simple string comparison. For more complex Regex comparisons, take a look at Groovy's Find and Match operators, which also work fine here.
Conclusion
I would prefer using this method (although I recommended it otherwise some time ago), as you can then check-in your pipeline configuration into your source control and you're more on the Configuration-as-Code approach as when you configure it in your Jenkins UI.


---

### Answer by username_2 on 2020-02-25. Score: 1

Simply delete the Jenkinsfile on the branch(es) you don't want to have corresponding branch jobs for. This will delete the branch job (of course, iff you have set your "Orphaned Item Strategy" appropriately).

From the perspective of a Jenkins Multibranch Pipeline Project, this has the same effect as deleting the branch. This is because it is simply scanning for branches that contain Jenkinsfiles as its criterion for when to create (or delete) a corresponding branch job.

Revert the commit to restore the Jenkinsfile if/when you need the branch job again.

> **Comments:**
> **username_3**: This is too much work. When you create a branch and delete a file you're altering GIT history for no good reason. It's much better to filter at the Jenkins level so you don't have to do anything.  
> **username_4**: @caseyvega you don't think enabling and disabling CI is worthy of a commit in history? This seems like the best answer to me.  
> **username_3**: OP is fairly clear why he doesn't want to do that. Nothing prevents you from managing Jenkins jobs and this setting via GitOps/JCasC. Secondly, what If I want to create a branch that Jenkins doesn't automatically pick up? Couple this with deleted files and messy pull requests/merges. This solution isn't wrong, it just doesn't scale or meet OP's requirements.  

---

