如何使用 Jenkins Git 插件接收本地 Git 分支名称? [英] How to receive local Git branch name with Jenkins Git plugin?

查看:28
本文介绍了如何使用 Jenkins Git 插件接收本地 Git 分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jenkins Git 插件 (v2.0) 在特定分支上运行构建,例如1.4.

I am using Branch Specifier option of Jenkins Git plugin (v2.0) to run build on specific branch, e.g. 1.4.

${GIT_BRANCH} 在这种情况下包含 origin/1.4 值.

${GIT_BRANCH} in this case contains origin/1.4 value.

如何接收用于克隆的本地 Git 分支的名称(即只有 1.4 没有 origin/ 前缀?

How can I receive a name of the local Git branch used for cloning (i.e. just 1.4 without origin/ prefix?

我已经尝试了签出特定的本地分支使用分支名称1.4的附加行为,但没有任何改变.

I've tried Check out to specific local branch Additional Behaviour with branch name 1.4, but nothing had changed.

我在 GitHub 上看到了相关的 PR,但被拒绝了(因为它仅通过 origin 远程修复了一种情况.

I've seen related PR on GitHub, but it was declined (as it fixes only one case with just origin remote).

推荐答案

你可以很容易地从变量中去除前缀​​:${GIT_BRANCH##origin/}

You can strip the prefix from the variable pretty easily: ${GIT_BRANCH##origin/}

虽然这不是一个非常通用的解决方案,但它非常简单,而且我还没有看到 Jenkins 克隆的存储库,它会使用 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.

更新:实际上,${GIT_BRANCH#*/} 适用于任何来源,甚至适用于包含斜杠的分支.# 执行非贪婪全局匹配,## 启用贪婪匹配.有关详细信息,请参阅 Bash 参考手册.

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.

这篇关于如何使用 Jenkins Git 插件接收本地 Git 分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆