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

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


I successfully used this syntax:

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


Looks like it's a Jenkins bug? You can grab in your build script the name of the checked out branch with this:

git symbolic-ref -q --short HEAD

Actually Jenkins has the working copy in detached HEAD, which is why git branch returns "no branch". See this quite detailed answer for digging into reconciliation with between a detached HEAD and a branch.


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

masterremotes/origin/HEAD -> origin/masterremotes/origin/develop