Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 386
Allen Pestaluky Avatar asked Apr 24 '18 22:04

Allen Pestaluky


People also ask

What is branch indexing?

Branch indexing discovers new or deleted branches. In the job configuration (Branch Sources > Property strategy), you can activate Suppress automatic SCM triggering, which will not automatically build new branches.

What is ENV Branch_name?

env.BRANCH_NAME: the branch name OR tag name of the current build, when it exists. env.GIT_BRANCH: same as BRANCH_NAME. env.TAG_NAME: the tag name of the current build, when it exists.

How do I create multiple branches in Jenkins?

Head over to your Jenkins instance and create a new item. Enter a name for the job, and select the “Multibranch Pipeline” option at the end of the screen. Then, click on the OK button. In the next screen, go to the “Branch sources” tab, click on the “Add source” button, and choose “Git” from the dropdown menu.

What does scan Multibranch pipeline do?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.


2 Answers

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.

like image 96
Majus Misiak Avatar answered Sep 21 '22 03:09

Majus Misiak


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.
like image 45
dushyant Avatar answered Sep 19 '22 03:09

dushyant