简体   繁体   中英

Jenkins Poll scm for specific branch with regex

I have two Jenkins pipeline jobs:-

  1. Main - Should trigger when there are code changes on main branch
  2. Branch - Should trigger when there are code changes on any branch other than the main branch

I have enabled the Poll SCM option for both the jobs that polls for changes every minute on the GitHub repo. Please note that polling is the only feasible option that can be used in my case.

Branch job config

在此处输入图像描述

As seen in the configuration of the Branch job it is looking for any branch using */* instead it should be looking for any branch except main branch. If I try to add any regex there like (?..*main).*$ then Jenkins gives an error as it can't parse the regex. Would really appreciate if someone can point out a way of making this work.

FYI: The Jenkinsfile has the pipeline stages - build, test and publish as checkout is handled from the pipeline config itself.

In the Additional Behaviours section there is an option called Strategy for choosing what to build .
When you add it you will have several options for the Choosing strategy , one of them is Inverse :

Build all branches except for those which match the branch specifiers configure above. This is useful, for example, when you have jobs building your master and various release branches and you want a second job which builds all new feature branches — ie branches which do not match these patterns — without redundantly building master and the release branches again each time they change.

So just add the option, set it to Inverse and set your Branch Specifier to */main - this will cause the job to run for any branch that is not the main branch (or any other branch specified in the pattern).

在此处输入图像描述


Another approach you can take is creating a single job for all branches, and then separate the different stages logic using the built-in branch condition for the when directive:

stage('Tests'){
   when { 
       branch 'master'
    }
    steps {
       ...
    }
}
stage('Upload'){
   when { 
       branch pattern: "release-\\d+", comparator: "REGEXP"
    }
    steps {
       ...
    }
}

This is very useful when most of the code is the same for all branches, but you have some additional stages for specific branches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM