Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the git branch name that was built by Jenkins when using inverse branch selection strategy?

We have one Jenkins job which builds every branch except master when there are new commits. This behavior can be configured via git plugin's 'choosing strategy:inverse' so that it listens to every branch except a specified branch.

This functions very nicely. But the problem is that GIT_BRANCH environment variable always refers to the excluded branch('origin/master' in our case). How to query the actual branch that was built by Jenkins?

Currently I am using a workaround that I grep it from generated changelog.xml. But it happens sometimes that changelog.xml is empty when Jenkins switches between different branches and I cannot find this info. What is the correct way of retrieving/querying from Jenkins the branch that was actually built?

like image 593
harish Avatar asked Feb 20 '13 16:02

harish


2 Answers

I successfully used this syntax:

GIT_BRANCH=`git rev-parse HEAD | git branch -a --contains | grep remotes | sed s/.*remotes.origin.//`
like image 142
Piotr Kuczynski Avatar answered Oct 07 '22 21:10

Piotr Kuczynski


Because git never checks out a branch just a commit directly you have to do the following:

To get the sha of the checked out commit:

git rev-parse HEAD

To get the all branches that commit is under:

git branch -a --contains SHA

The out put of the second command could look like this

master
remotes/origin/HEAD -> origin/master
remotes/origin/develop
like image 33
Daniel Little Avatar answered Oct 07 '22 19:10

Daniel Little