Jenkins Git Publisher:如何在构建后将代码提交回 master?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17731875/
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-19 08:47:50  来源:igfitidea点击:

Jenkins Git Publisher: How to commit code back to master after build?

androidgitjenkinsjenkins-pluginsgit-push

提问by Karim Varela

I'm having some difficulty with Jenkins Git Publisher committing and pushing code back to master after my build. My build process increases a version number in one of my files and then I want to commit this file back into the repo, but I can't seem to get it to work.

我在 Jenkins Git Publisher 提交和在我的构建后将代码推送回 master 时遇到了一些困难。我的构建过程在我的一个文件中增加了一个版本号,然后我想将此文件提交回 repo,但我似乎无法让它工作。

In Source Code Management->Git, these are my settings:

在 Source Code Management->Git 中,这些是我的设置:

  • Repository Name: Android
  • Branch Specifier: master
  • Checkout/merge to local branch: master
  • 存储库名称:Android
  • 分支说明符:master
  • 结帐/合并到本地分支:master

Then, in Git Publisher, these are my settings:

然后,在 Git Publisher 中,这些是我的设置:

  • Push Only If Build Succeeds: checked
  • Merge Results: checked
  • Branch to push: master
  • Target remote name: Android
  • Notes: Note to push: Updating version
  • Notes: Target remote name: Android
  • Notes: Note's namespace: master
  • 仅在构建成功时推送:选中
  • 合并结果:选中
  • 要推送的分支:master
  • 目标远程名称:Android
  • 注意事项:推送注意事项:更新版本
  • 备注:目标远程名称:Android
  • 笔记:笔记的命名空间:master

This is the output from Jenkins:

这是詹金斯的输出:

Pushing HEAD to branch master at repo Android
Adding note to namespace "master":
Updating version

Please help!

请帮忙!

采纳答案by Maciej ?opaciński

I think jenkins git publisher plugin is not doing anything like

我认为 jenkins git 发布者插件没有做任何类似的事情

git add .
git commit -m 'xxx'

Plugin only perform push and optionally add note using git-notes.

插件仅执行推送并可选地使用git-notes添加注释

See notes here:

请参阅此处的注释:

https://github.com/hamsterready/jenkins-git-publisher-test/tree/refs/notes/master

https://github.com/hamsterready/jenkins-git-publisher-test/tree/refs/notes/master

To achieve something like this: https://github.com/hamsterready/jenkins-git-publisher-test/commit/d80a1eef2133bee6d7a57b1b229ccd5990d2d606

要实现这样的目标:https: //github.com/hamsterready/jenkins-git-publisher-test/commit/d80a1eef2133bee6d7a57b1b229ccd5990d2d606

I have added post-build step (execute shell script) with:

我添加了构建后步骤(执行 shell 脚本):

git add .
git commit -m 'Updating git.properties'

And then enabled git publisher post-build action which pushed local commit to origin.

然后启用将本地提交推送到源的 git 发布者构建后操作。

回答by Matthias Braun

If you are using also Gradle for your builds, there is a Git pluginfor it.

如果您也在构建中使用 Gradle,则可以使用Git 插件

Here is the complete build.gradle:

这是完整的build.gradle

buildscript {
  repositories { mavenCentral() }
  dependencies { classpath "org.ajoberstar:gradle-git:0.6.3" }
}
import org.ajoberstar.gradle.git.tasks.*

task tag(type: GitTag) {
    tagName = version
    message = "Release of $version"
}

task pushWithTags(type: GitPush){
    credentials{
        username = "karim"
        password = gitPassword
    }
    setPushTags(true)
}
task add(type: GitAdd){
    include("yourVersionFile.txt") 
    // or add everything with include("*") 
}
task commit(type: GitCommit){
    setMessage(commitMsg)
}
task pushNewVersion(){
    tasks.add.execute()
    tasks.commit.execute()
    tasks.tag.execute()
    tasks.pushWithTags.execute()
}

This is how you add, tag, commit, and push using the script (there is a pluginfor doing that from within Jenkins):

这是您使用脚本添加、标记、提交和推送的方式(Jenkins 中有一个插件可以执行此操作):

gradle pushNewVersion "-PcommitMsg=hi" "-Pversion=0.1.1" "-PgitPassword=secret"

回答by Swetha

I experienced the same issue for pushing back the changes to origin using Git plugin on Jenkins. What is the Git client plugin version you are using? There must be a bug in git client plugin and thus the behavior.

我在 Jenkins 上使用 Git 插件将更改推回原点时遇到了同样的问题。您使用的 Git 客户端插件版本是什么?git 客户端插件和行为中一定存在错误。

They fixed the issue related to push sometime back. Ref: https://issues.jenkins-ci.org/browse/JENKINS-17242. And seems like its broken, as there is another bug filed for the same recently again: https://issues.jenkins-ci.org/browse/JENKINS-19442

他们修复了与推回相关的问题。参考:https: //issues.jenkins-ci.org/browse/JENKINS-17242。似乎它坏了,因为最近又为同一个问题提交了另一个错误:https: //issues.jenkins-ci.org/browse/JENKINS-19442

If you read the discussion in the urls, the quick fix suggested is to downgrade Git client plugin to 1.0.5 and git plugin to 1.3.0. Hope this works for you.

如果您阅读了 url 中的讨论,建议的快速修复是将 Git 客户端插件降级到 1.0.5,将 git 插件降级到 1.3.0。希望这对你有用。