Jenkins + Git Publisher - 如何推回 {GIT_BRANCH}?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14766214/
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
Jenkins + Git Publisher - How to push back to {GIT_BRANCH}?
提问by mtutty
I'm using the current version of the Git plugin (SCM provider and Publisher) on Jenkins 1.500. The SCM polling works fine, using my git HTTP URL and "branches to build" setting of "feature-*". This picks up changes to any branch, e.g., "feature-1234", runs the build/test/coverage tasks, and reports on success or failure. All of this works fine, including a merge from the integration branch where the code should end up after a successful build and code review.
我在 Jenkins 1.500 上使用当前版本的 Git 插件(SCM 提供者和发布者)。SCM 轮询工作正常,使用我的 git HTTP URL 和“功能-*”的“构建分支”设置。这会获取对任何分支的更改,例如“feature-1234”,运行构建/测试/覆盖任务,并报告成功或失败。所有这些都工作正常,包括来自集成分支的合并,在成功构建和代码后,代码应该在该分支结束。
The problem is in trying to push the completed build branch BACK to origin, on the same "feature-1234" branch. The build variable "GIT_BRANCH" in this case contains "origin/feature-1234", which produces the following error and failure in the Git Publisher after an otherwise successful build:
问题在于试图将完成的构建分支推回原点,在同一个“feature-1234”分支上。在这种情况下,构建变量“GIT_BRANCH”包含“origin/feature-1234”,它在构建成功后在 Git Publisher 中产生以下错误和失败:
Pushing HEAD to branch origin/feature-1234 at repo origin
ERROR: Failed to push branch origin/feature-1234 to origin
hudson.plugins.git.GitException: Command "/usr/bin/git push https://jenkins:[email protected]/git/project HEAD:origin/feature-1234" returned status code 1:
stdout:
stderr: error: unable to push to unqualified destination: origin/feature-1234
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'https://jenkins:[email protected]/git/project'
at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:897)
at hudson.plugins.git.GitAPI.launchCommand(GitAPI.java:858)
at hudson.plugins.git.GitAPI.push(GitAPI.java:915)
at hudson.plugins.git.GitPublisher.invoke(GitPublisher.java:351)
at hudson.plugins.git.GitPublisher.invoke(GitPublisher.java:333)
at hudson.FilePath.act(FilePath.java:865)
at hudson.FilePath.act(FilePath.java:838)
at hudson.plugins.git.GitPublisher.perform(GitPublisher.java:333)
at hudson.tasks.BuildStepMonitor.perform(BuildStepMonitor.java:36)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:810)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:785)
at hudson.model.Build$BuildExecution.post2(Build.java:183)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:732)
at hudson.model.Run.execute(Run.java:1582)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:236)
Build step 'Git Publisher' changed build result to FAILURE
Build step 'Git Publisher' marked build as failure
See the extra "origin" in there? origin/feature-1234<== that's the current value of ${GIT_BRANCH}, and while I understand it's a remote branch and all, it's stopping me from running the process we want to follow.
看到那里额外的“起源”了吗?origin/feature-1234<== 这是 ${GIT_BRANCH} 的当前值,虽然我知道它是一个远程分支等等,但它阻止我运行我们想要遵循的进程。
If I'm missing something simple, I'd love to hear it. But I have tried many different settings for the various git-related portions of my build and nothing seems to allow me to commit the merged and tested code back to the work branch (as opposed to the integration branch, which is easy to do.
如果我遗漏了一些简单的东西,我很想听听。但是我已经为我构建的各个 git 相关部分尝试了许多不同的设置,似乎没有什么能让我将合并和测试的代码提交回工作分支(与集成分支相反,这很容易做到)。
回答by Marius K
A workaround for me seems to push the branch manually, at least when it is going to be the same each time.
对我来说,一种解决方法似乎是手动推送分支,至少在每次都相同时。
回答by kazerm
In the build configuration, "Source Code Management" => Advance There is a field "Name" You need to specify the ID of the repository and then to use it at the git publisher plugin: "Target remote name"
在构建配置中,"Source Code Management" => Advance 有一个字段"Name" 需要指定仓库的ID,然后在git发布者插件中使用:"目标远程名称"
Those two names need to be identical look at the "?" - to get more information
这两个名字需要相同,看看“?” - 获取更多信息
回答by cedd
I push changes by hand. You can use the windows environment variable as below in batch scripts:
我用手推动变化。您可以在批处理脚本中使用 Windows 环境变量,如下所示:
This evaluates to eg. 'git checkout feature-1234'
这评估为例如。'git 结帐功能-1234'
git checkout %GIT_BRANCH:origin/= %
This evaluates to eg. 'git push origin feature-1234'
这评估为例如。'git push origin feature-1234'
git push %GIT_BRANCH:/= %
You can also do something similar with token macros such as ${GIT_BRANCH##origin/}
and ${GIT_BRANCH#*/}
. These work in some Jenkins plugins, but not all, so it might not work in the Git Publisher.
您还可以使用令牌宏执行类似的操作,例如${GIT_BRANCH##origin/}
和${GIT_BRANCH#*/}
。这些适用于某些 Jenkins 插件,但不是全部,因此它可能不适用于 Git Publisher。
回答by kubanczyk
I recommend to configure fully qualified /refs/remotes/origin/feature-1234
as your Git "Branch to build". This way it seems more understandable to me.
我建议将完全限定配置/refs/remotes/origin/feature-1234
为您的 Git“要构建的分支”。这样对我来说似乎更容易理解。
In such case your $GIT_BRANCH would get magically set by Jenkins to origin/feature-1234
same as you observed.
在这种情况下,您的 $GIT_BRANCH 会被 Jenkins 神奇地设置为origin/feature-1234
与您观察到的相同。
Then, instead of using overly complicated (but portable) GitPublisher, just add a post-build step "Execute Shell":
然后,不要使用过于复杂(但可移植)的 GitPublisher,只需添加一个构建后步骤“执行 Shell”:
echo Remote branch is $GIT_BRANCH, replacing origin with refs/heads.
git push --follow-tags "$GIT_URL" "+HEAD:${GIT_BRANCH/#origin\//refs/heads/}"
回答by Sahitya Pasnoor
In the build section Add "Execute Shell Script" use this shell script:
在构建部分添加“执行 Shell 脚本”,使用这个 shell 脚本:
echo current Branch is ${GIT_BRANCH#*/}
git checkout ${GIT_BRANCH#*/}
git commit -am 'Your commit message'
git push origin ${GIT_BRANCH#*/}
This should be the last step in the build section.
这应该是构建部分的最后一步。
回答by Sebastian Zavadschi
First, look for GIT_LOCAL_BRANCH
variable
(https://plugins.jenkins.io/git/#branch-variables)
首先,寻找GIT_LOCAL_BRANCH
变量(https://plugins.jenkins.io/git/#branch-variables)
In my case it was missing, so I extracted it from GIT_BRANCH
variable, as a dirty workaround
在我的情况下它丢失了,所以我从GIT_BRANCH
变量中提取它,作为一个肮脏的解决方法
$ GIT_LOCAL_BRANCH=$(echo ${GIT_BRANCH} | awk -F\/ '{print }')
$ echo $GIT_LOCAL_BRANCH
feature-1234