15

We have some code in git and I started setting up Jenkins to grab our branches and try a compile. It seems that a few of the branches may have begun to rot in the years since they were last built, as they fail to finish a make.

I would like to build all branches that are found, except for a list of excluded ones. Is this possible in Jenkins? This will let me get things up and running, then come back to enable more branches as I try to fix them.

What I have tried so far

Regex with lookahead

Looking at the 'Git > Branches to build' option I was hopeful that I could replace the default '**' wildcard with a :. A bit of digging about and double checking with http://rubular.com/ suggested that the following might do what I wanted.

:^(?!origin/exclude\-this\-branch\.v1|origin/exclude\-this\-branch\-too.v2)(\S+)

Now there is an assumption here about the regex engine running in the background. I would hope it might understand lookahead, but if it does not that explains why this method fails. It seems to build all the branches, including the one I am try to exclude. Maybe I just need to find some more debug?

Looked for similar questions here

I came across Jenkins/Hudson Build All Branches With Prioritization which seemed to contain a possible solution in that some added an inverse option to branch matching https://github.com/jenkinsci/git-plugin/pull/45 sounds like what I need. Sadly this does not seem to be in the version of Jenkins I have, which is odd as 2011 is a long time ago.

The System I Am Using

Ubuntu LTS 14.04. Jenkins ver. 1.611. GNU tool chains to make C/C++ code.

Community
  • 1
  • 1
TafT
  • 2,764
  • 6
  • 33
  • 51
  • 2
    This is pretty much a [duplicate of this question](https://stackoverflow.com/questions/21314632/how-to-exclude-git-branch-from-building-in-jenkins). You should write a regex that *matches* only the branches you want to exclude (or simpler would be to add multiple "Branch to build" entries), and then use the "Inverse" choosing strategy. – Christopher Orr May 07 '15 at 12:30
  • @ChristopherOrr that sounds like the solution I need. I just could not find out where the inverse options was hidden. I have tried it out and it seems to not work, still tries to build the excluded branch but I will persist. Should you put your comment as an answer so that I can select it or is that not how duplicates are handled? – TafT May 07 '15 at 12:43

2 Answers2

16

How about using

^(?!.*master).*$

as branch specifier. This regexp means all branches not matching master.

Breakdown:

^(?!.*master).*$
^                ---> beginning of string
 (?!        )    ---> negative lookahead find all that doesnt match
    .*           ---> zero or more of 'any' character
      master     ---> should not match master
             .*$ ---> will match anything to the end of string

Related: https://stackoverflow.com/a/18709097/109305

Community
  • 1
  • 1
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • 1
    I stated in the question that I had tried this and it was not working. Maybe the RegEx engine has been updated in the past two years so that it works now. I ended up doing the Inverse choosing strategy as suggested by ChristopherOrr. – TafT May 08 '17 at 08:07
  • how to use ignore for two branches such a master and feature branches – salsinga Dec 17 '19 at 10:25
  • Often, look-ahead (or look-behind) assertions only work with fixed-length strings. Also, in this particular case I believe that you cannot use '|' to combine the things you want to exclude - instead, you must define multiple negative look-ahead assertions in a row. Reference: https://stackoverflow.com/a/2078924/2773846 – dpmott Jan 08 '21 at 04:22
  • so if i take out ?!, then it will match any branches containing master? – ealeon Jul 28 '21 at 15:52
8

I needed using the Jenkins tool "filter branch by regex", and I discovered flavor of that regex works just with back reference. So, following the Jesper response and this issue, here I did two more examples:

^(?:.*release\/\d+.\d+.\d+(?!.))$
// check all branches like "release/0.0.0" or "origin/release/1.2.3"
^(?:.*release\/\d+.\d+.\d+_any)$
// check all branches like "release/0.0.0_any" or "origin/release/1.2.3_any"

I hope this would helpful for someone

EDIT - New example

^(?:origin\/develop|origin\/master|origin\/release\/\d+\.\d+\.\d+(?!.)|origin\/release\/\d+\.\d+\.\d+(?:_uat|_preprod))$

or

^(?:.*develop|.*master|.*release/\d+\.\d+\.\d+(?!.))$
Luke Cottage
  • 81
  • 1
  • 4
  • What is the purpose of those `?:` parts? Shouldn't the first regex simple be `^.*release\/\d+.\d+.\d+$`? – Aaron Digulla Jul 04 '18 at 08:49
  • Hi Aaron, I give you the code grant where I'm using those regexes: `triggers { gitlab(triggerOnPush: true, triggerOnMergeRequest: false, branchFilterType: "RegexBasedFilter", targetBranchRegex: '^(?:(?!feature).)*$') }` if you don't use it, that trigger doesn't work. – Luke Cottage Jul 11 '18 at 13:28