如何使用 Jenkins Git 插件接收本地 Git 分支名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20541609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:27:51  来源:igfitidea点击:

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

gitjenkins

提问by Max Romanovsky

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

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

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

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

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

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

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

我已经尝试Check out to specific local branch使用分支名称的附加行为1.4,但没有任何改变。

I've seen related PRon GitHub, but it was declined (as it fixes only one case with just originremote).

我在 GitHub 上看到了相关的PR,但它被拒绝了(因为它只修复了一个origin远程问题)。

回答by Jan V?elák

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

您可以很容易地从变量中去除前缀​​: ${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 originfor the remote name.

尽管这不是一个非常通用的解决方案,但它非常简单,而且我还没有看到 Jenkins 克隆的存储库,它会使用除origin远程名称之外的其他内容。

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 Manualfor details.

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

回答by Sean Connolly

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 branchin the Git Plugin, instead of any macro/variable.

截至 2016 年 3 月(Git Plugin v2.4.3),现在已本地支持。简单地将其**作为Check out to specific local branchGit 插件中的值,而不是任何宏/变量。

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.

如果选中,并且其值为空字符串或“**”,则分支名称是从没有源的远程分支计算的。在这种情况下,远程分支 origin/master 将检出到名为 master 的本地分支,而远程分支 origin/develop/new-feature 将检出到名为 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.

这是由@roostergx 和@NickVolynkin 在发布时提到的引入的。我只是在这里记录,希望能在未来帮助人们。

回答by burcakulug

I managed to do that with a couple steps:

我设法通过几个步骤做到了这一点:

  1. Add Execute shellpre 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 variablespre step for build.propertiesfile

  1. 添加Execute shell预步骤:

    git_branch_local=$(echo $GIT_BRANCH | sed -e "s|origin/||g") echo GIT_BRANCH_LOCAL=$git_branch_local > build.properties

  2. Inject environment variablesbuild.properties文件添加一个前置步骤

then you should have a GIT_BRANCH_LOCALenvironment variable that doesn't have origin/

那么你应该有一个没有的GIT_BRANCH_LOCAL环境变量origin/

回答by Tóth Krisztián Gyula

@Chris answer helped me. Thank you!

@Chris 的回答帮助了我。谢谢!

Don't forget to add the "Additional Behavior -> Check out to specific local branch" under "Source Code Management".

不要忘记在“源代码管理”下添加“附加行为-> 签出到特定的本地分支”。

enter image description here

在此处输入图片说明

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)

直到我没有添加 ${GIT_LOCAL_BRANCH} 变量在我尝试使用它时不可用。(例如:执行 shell 命令 'printenv' 然后检查控制台输出)

回答by Tim Webster

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)

如果您将其作为本地分支签出,然后使用 ${GIT_LOCAL_BRANCH} 它会提供本地分支名称(即“develop”而不是“origin/develop”。(Git 插件版本 3.0.0)

回答by roostergx

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.

我向 jenkins-ci 组(https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/jenkinsci-dev/xuweZbwn9zE/BAWfs-ZeFAAJ)发布了一条消息,建议增强 Git 插件“签出到本地分支”选项。默认情况下,git 插件会检查一个分离的头部。本地分支允许您指定特定的分支名称。配置时,使用此方法,本地分支名称与指定的完全相同。

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.

如果使用Jenkins做maven发布,本地分支名称必须与远程分支名称一致。有关这方面的更多信息,请参阅发布到 google 群组的帖子。

To address this requirement, I've created a pull request (https://github.com/jenkinsci/git-plugin/pull/381) with code and test cases.

为了满足这一要求,我创建了一个包含代码和测试用例的拉取请求 ( https://github.com/jenkinsci/git-plugin/pull/381)。

Also created a JIRA issue (https://issues.jenkins-ci.org/browse/JENKINS-33202) to document the problem.

还创建了一个 JIRA 问题(https://issues.jenkins-ci.org/browse/JENKINS-33202)来记录问题。

If anyone is in need of this feature, I encourage you post to the group discussion, and to vote for the JENKINS-33202.

如果有人需要此功能,我鼓励您在小组讨论中发帖,并为 JENKINS-33202 投票。

回答by Steven Collins

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.

虽然这是真的,但正如 dtabuenc 在对 Jan V?elák 的回答的评论中所说的那样,如果您试图在 Execute Shell 构建步骤(或类似的),您仍然可以使用相同的想法。

TEMP_GIT_BRANCH=${GIT_BRANCH}
MY_GIT_BRANCH="${TEMP_GIT_BRANCH#*/}"

works fine (and that's valid POSIX, no bash required).

工作正常(这是有效的 POSIX,不需要 bash)。

回答by fangmobile.com

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

我今天遇到了这个问题。原因是 Jenkins git 插件会将“origin/”添加到您提供给 GIT_BRANCH 构建参数的任何分支名称。见 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"

git 插件设置了几个可以在脚本中使用的环境变量: GIT_BRANCH - 当前使用的分支的名称,例如“master”或“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_BRANCH = foo 构建时,Jenkins 只会在其前面添加“origin/”。然后,当你做

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.

为了避免这种情况,您可以做的是在构建参数中使用其他内容,例如 GIT_BRANCH_NAME 或其他任何内容,只是不要使用 GIT_BRANCH。在配置中,删除 GIT_BRANCH 参数,并添加另一个名为 GIT_BRANCH_NAME 的参数。然后在“带参数的构建”中,在 GIT_BRANCH_NAME 下,指定您的分支。

回答by webofmars

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']]])
}

回答by Josh

Based on this SO answer: How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?

基于这个 SO 答案:How do I get the output of a shell command used 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):

我使用了这个,最后能够让它工作(下面的例子假设分支变量为$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: '[email protected]:......git'
}