20

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?

3 Answers 3

29

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

jenkins filter branches

4
  • Any way to do this within a Jenkinsfile (so it's reproducible)?
    – xjcl
    Feb 18, 2020 at 22:42
  • 2
    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 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
    Feb 26, 2020 at 12:45
  • 1
    @biolauri You comment would be worth its own answer.
    – not2savvy
    Oct 2, 2022 at 12:00
  • @not2savvy: I did: 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
    Oct 5, 2022 at 21:50
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.

3
  • 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.
    – casey vega
    Apr 24, 2020 at 16:05
  • @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
    Jun 1, 2020 at 20:49
  • 1
    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
    Jun 14, 2020 at 18:40
1

Instead of configuring it in the UI (as perfectly described by casey vega'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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.