Jenkins Multibranch Config: How to Filter branches based on variable string?
Asked Answered
B

2

25

We have Jenkins set up with 7 multibranch pipeline projects, each building off the same git repo, but for different target platforms. Each of these multibranch pipelines builds a number of branches. We currently set which branches each multibranch pipeline builds by using the following property in the multibranch project configuration:

Branch Sources -> Git -> Behaviors -> Filter by name (with wildcards)

Currently, each multibranch pipeline has the same string of branches in this Filter by name (with wildcards) field. Each time we want Jenkins to start building a new branch, we go through all 7 multibranch project configurations and update this field to include the new branch.

It's a bit of a pain to go through each configuration and change this field every time, since we always want each configuration to have the same list of branches. Is it possible to simply use some type of a variable in this field? This way we only would need to change one location instead of trying to keep 7 different configurations in sync with each other, which is prone to error and also a bit of a pain.

Thanks for your help! Allen

Boyish answered 24/4, 2018 at 22:24 Comment(0)
P
38

Rather than filtering with wildcards, you could try filtering branches with regular expression. In our case, pattern like:

(master|develop|release.*|feature.*|bugfix.*)

has been working well to cover the repository. That is, assuming that you follow Git Flow or similar methodology. Unfortunately, there is no simple way to sync the configuration between MultiBranch Pipelines build from one repository. Neither Multibranch Pipeline, nor Organization plugins are designed to work with Multiple Jenkinsfiles.

Also, you can try to sync only the branch configuration between Projects using Jenkins script console. Most of the Job configuration does not have to be set on Project level. For instance, you can create shared script (or shared library) to would be sourced by other jobs, to set the same job properties on each of them. See How do you load a groovy file and execute it for details.

Purgatory answered 25/4, 2018 at 10:17 Comment(4)
Thanks! Using the Script Console to execute a script that will apply the settings I want to a set of multibranch pipelines sounds like exactly the kind of helper I'm looking for. (This way it would be execute once and no chance of accidentally missing a project).Boyish
Unfortunately, I've only done the tiniest bit of scripting in Jenkins, so I've got a bit of learning to do before I can write a script that will apply a string to the Filter by name (with wildcards) field of a number of projects... Any additional help here would be appreciated...Boyish
Well, Jenkins script console is your best friend if you want to do anything more complex. Though MultiBranch Project configuration can be a bit harsh place to start :-) WorkflowMultiBranchProject provides very rich API for scripting as a starting point, for instance, with Jenkins.instance.getItem("YourMultiBranch").getSCMSources() How do you go from there will depend on exact source (GitHub, Bitbucket).Purgatory
All in all this may be a bit of a hassle for single repository only, but feel free to ask another question if you encounter any problem.Purgatory
A
14

if you want to use the wildcard you can provide like below:

adding wildcard for images

In this example it will discover only qa and dev branch.

NOTE: You have to use "Discover branches" also with "filter by name (with wildcards)" behaviour.
Accrete answered 22/5, 2019 at 6:28 Comment(1)
Thank you for mentioning abut Discover branches. This solved my problem!Embrey

© 2022 - 2024 — McMap. All rights reserved.