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
As of March 2016 (Git Plugin v2.4.3) this is now supported natively. Simply put **
as the value for Check out to specific local branch
in the Git Plugin, instead of any macro/variable.
The description for the field details the new behavior:
If selected, and its value is an empty string or "**", then the branch name is computed from the remote branch without the origin. In that case, a remote branch origin/master will be checked out to a local branch named master, and a remote branch origin/develop/new-feature will be checked out to a local branch named develop/newfeature.
This was introduced by @roostergx & @NickVolynkin mentioned when it was released. I'm just documenting here to hopefully help folks in the future.
You can strip the prefix from the variable pretty easily: ${GIT_BRANCH##origin/}
Although this is not a very general solution, it's quite simple and I haven't seen a repository cloned by Jenkins, which would use something else than origin
for the remote name.
Update: Actually, ${GIT_BRANCH#*/}
will work for any origin and even for branches containing slashes. The #
does non-greedy glob matching, ##
enables greedy matching. See Bash Reference Manual for details.
I ran into this problem as well to day. It seems to be that when I change the value of Check out to specific local branch
, I need to delete the local repo on the build server before triggering a new build.
I managed to do that with a couple steps:
Add Execute shell
pre step with these:
git_branch_local=$(echo $GIT_BRANCH | sed -e "s|origin/||g")
echo GIT_BRANCH_LOCAL=$git_branch_local > build.properties
Add an Inject environment variables
pre step for build.properties
file
then you should have a GIT_BRANCH_LOCAL
environment variable that doesn't have origin/
In a pipeline you can do it like this :
// get arround a git scm anoying feature
stage ('Bootstrap') {
sh "echo GIT_BRANCH_LOCAL=\\\"$GIT_BRANCH\\\" | sed -e 's|origin/||g' | tee version.properties"
}
stage('Checkout') {
load('version.properties')
checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "${GIT_BRANCH_LOCAL}"]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/webofmars/grails-website.git']]])
}
After long time I finally got the solution for my case to get local git name (for SonarQube) without "origin/" in my Jenkins freestyle project (not Jenkinsfile). I am using Windows environment.
In Jenkins:
**
/d:sonar.branch.name=${GIT_LOCAL_BRANCH}
So only with this combination of **
and ${GIT_LOCAL_BRANCH}
it worked for me.