How do I configure Jenkins to build all branches e

2020-08-16 18:20发布

问题:

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.

回答1:

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



回答2:

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+(?!.))$