I am using Branch Specifier option of Jenkins Git plugin (v2.0) to run build on specific branch, e.g. 1.4
.
${GIT_BRANCH}
in this case conta
I posted a message to the jenkins-ci group (https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/jenkinsci-dev/xuweZbwn9zE/BAWfs-ZeFAAJ) suggesting an enhancement to the Git plugin "Check Out to Local Branch" option. By default, the git plugin checks out a detached head. The Local Branch allows you to specify a specific branch name. When configured, using this method, the local branch name is exactly as specified.
If you are using Jenkins to do a maven release, it is mandatory that the local branch name is the same as the remote branch name. See the post to google groups for more on this.
To address this requirement, I've created a pull request (https://github.com/jenkinsci/git-plugin/pull/381) with code and test cases.
Also created a JIRA issue (https://issues.jenkins-ci.org/browse/JENKINS-33202) to document the problem.
If anyone is in need of this feature, I encourage you post to the group discussion, and to vote for the JENKINS-33202.
If you check it out as a local branch and then use ${GIT_LOCAL_BRANCH} it gives the local branch name (i.e. 'develop' instead of 'origin/develop'. (Git Plugin version 3.0.0)
Based on this SO answer: How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
I used this, and finally was able to get it to work (the below example assumes a branch variable of $Branch
):
stage('Checkout') {
GIT_BRANCH_LOCAL = sh (
script: "echo $Branch | sed -e 's|origin/||g'",
returnStdout: true
).trim()
echo "Git branch: ${GIT_BRANCH_LOCAL}"
git branch: "${GIT_BRANCH_LOCAL}", credentialsId: '...', url: 'git@github.com:......git'
}
@Chris answer helped me. Thank you!
Don't forget to add the "Additional Behavior -> Check out to specific local branch" under "Source Code Management".
Until I did not add that the ${GIT_LOCAL_BRANCH} variable was not availabe when I tried use it. (E.g.: execute shell command 'printenv' then check Console output)
I ran into the problem today. The reason is Jenkins git plug-in will add "origin/" to any branch name you supplied to GIT_BRANCH build parameter. See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin
The git plugin sets several environment variables you can use in your scripts: GIT_BRANCH - Name of the branch currently being used, e.g. "master" or "origin/foo"
As a result, when you build with parameter GIT_BRANCH = foo, Jenkins will just add "origin/" in front of it. Then, when you do
git check out ${GIT_BRANCH}
you are actually doing
git check out origin/foo
What you can do to avoid this, is using something else in your build parameter, such as GIT_BRANCH_NAME or what ever else, just do not use GIT_BRANCH. In configuration, delete the GIT_BRANCH parameter, and add another one with name GIT_BRANCH_NAME. Then in "Build with parameter", under GIT_BRANCH_NAME, specify your branch.
While it's true, as dtabuenc says in a comment to Jan Včelák's answer, that shell syntax isn't going to work on token macros directly, if you are trying to get at the branch name in an Execute Shell build step (or similar), you can still use the same idea.
TEMP_GIT_BRANCH=${GIT_BRANCH}
MY_GIT_BRANCH="${TEMP_GIT_BRANCH#*/}"
works fine (and that's valid POSIX, no bash required).