10

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?

18

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

jenkins filter branches

| improve this answer | |
  • Any way to do this within a Jenkinsfile (so it's reproducible)? – xjcl Feb 18 at 22:42
  • 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 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 at 12:45
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.

| improve this answer | |
  • 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 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 at 20:49
  • 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 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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