How to get the git branch name that was created by Jenkins using the reverse strategy selection strategy?

We have one Jenkins assignment that builds every branch except the master when new commits appear. This behavior can be configured using the git plugin 'by selecting the: inverse' strategy so that it listens to every branch except the specified branch.

It is very comfortable. But the problem is that the GIT_BRANCH environment variable always refers to the excluded branch ("origin / master" in our case). How to request the actual branch created by Jenkins?

I am currently using a workaround that I grep it from the generated changelog.xml file. But sometimes it happens that changelog.xml is empty when Jenkins switches between different branches, and I can not find this information. What is the correct way to get / request from an existing Jenkins branch?

+6
source share
4 answers

Since git never checks the branch only commit itself, you should do the following:

To get an assessment of a verified commit:

git rev-parse HEAD 

To get all the branches that commit, are under:

 git branch -a --contains SHA 

The output of the second command might look like this:

 master remotes/origin/HEAD -> origin/master remotes/origin/develop 
+2
source

I have successfully used this syntax:

 GIT_BRANCH=`git rev-parse HEAD | git branch -a --contains | grep remotes | sed s/.*remotes.origin.//` 
+17
source

Sounds like a Jenkins bug? You can capture the name of the selected branch in your build script with this:

 git symbolic-ref -q --short HEAD 

In fact, Jenkins has a working copy in a separate HEAD, so git branch returns โ€œno branchโ€. See this rather detailed answer for delving into the agreement between an individual head and branch.

+2
source

I canโ€™t believe how difficult it is. I do this for Jenkins too. I put the decision of Peter Kuchinsky:

 branch=`git rev-parse HEAD | git branch -a --contains | grep remotes | sed s/.*remotes.origin.//` branch=`echo $branch | awk '{print $NF}'` 

Because sometimes, as Matt Cantor noted, Piotraโ€™s decision gives a lot of rubbish. But the last word in this junk always seems to be right. Please note that this solution only works if the ref you are using matches the branch that exists on the remote computer (therefore, the local branches do not work).

0
source

All Articles