I would like to add the current git branch to properties during the build process of my project.
My project can either be built locally or using Jenkins. When the project is built locally this is quite straightforward, I have the following task defined that gets the branch successfully:
task getBranch {
def cmd = "git rev-parse --abbrev-ref HEAD"
def proc = cmd.execute()
def branch = proc.text.trim()
ext.branch = branch
}
However, part of the Jenkins build process involves checking out the last commit on a branch into its own branch, leaving the project in a detached head state:
commit_hash=${git rev-parse refs/remotes/origin/BRANCH^{commit}}
git checkout -f ${commit_hash}
At this point I can run git reflog show -n1
to get:
HEAD@{0}: checkout: moving from BRANCH to $commit_hash
I'm going to have to write some groovy
code to get the value of BRANCH from a string in that format. However, I was wondering if there was a more straightforward way to get BRANCH from git?