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 contains origin/1.4
value.
How can I receive a name of the local Git branch used for cloning (i.e. just 1.4
without origin/
prefix?
I've tried Check out to specific local branch
Additional Behaviour with branch name 1.4
, but nothing had changed.
I've seen related PR on GitHub, but it was declined (as it fixes only one case with just origin
remote).
If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With