How to receive local Git branch name with Jenkins Git plugin?

Solution 1:

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.

Solution 2:

I managed to do that with a couple steps:

  1. 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

  2. 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/