bash 仅当有更改时,如何让 Jenkins git 提交?

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

How to let Jenkins git commit only if there are changes?

gitbuildjenkinsbash

提问by s.d

I have a Jenkins job that builds from a github.com repository's master branch with Maven (mvn clean install), then checks for license headers in Java files and missing NOTICE files, and adds them if necessary (mvn license:format notice:generate). Sometimes this will result in changed or added files, sometimes not.

我有一个 Jenkins 作业,它使用 Maven ( mvn clean install)从 github.com 存储库的主分支构建,然后检查 Java 文件中的许可证头和缺少的 NOTICE 文件,并在必要时添加它们 ( mvn license:format notice:generate)。有时这会导致更改或添加文件,有时不会。

Whenever there have any changes been made (by the license plugin) I want to push the changes to the github repo.

每当(通过许可证插件)进行任何更改时,我都想将更改推送到 github 存储库。

Now I'm having trouble figuring out how best to achieve that. I've added a shell build step after the Maven license plugin where I execute git commands:

现在我很难弄清楚如何最好地实现这一目标。我在执行 git 命令的 Maven 许可证插件之后添加了一个 shell 构建步骤:

git add . # Just in case any NOTICE files have been added
git commit -m "Added license headers"

git add .alone works, i.e., doesn't break the build, even if no files have been added. However, git commitbreaks the build, if there aren't any changes at all.

git add .单独工作,即不会破坏构建,即使没有添加任何文件。但是,git commit如果根本没有任何更改,则会中断构建。

I'm not concerned about pushing back to github, as I believe the Git Publisher post-build action can do this for me. Can someone please point me in the right direction for the git commit?

我不担心推回 github,因为我相信 Git Publisher 构建后操作可以为我做到这一点。有人可以为我指出正确的 git commit 方向吗?

采纳答案by s.d

To stop the build from breaking on the shell build step returning exit code 1at any one point, e.g., when trying to make a git commit although there is nothing to commit, you can simply wrap the respective commands into an echo.

要阻止构建在 shell 构建步骤1中的任何一点返回退出代码,例如,当尝试进行 git commit 时,尽管没有任何内容可提交,您可以简单地将相应的命令包装到 echo 中。

echo `git add -A && git commit -m "Added license headers"`

Now, whether there are untracked files to add to the index or not, and whether the working tree is dirty or clean, echo will return exit code 0, as there will be some string to be echoed.

现在,无论是否有未跟踪的文件要添加到索引中,以及工作树是脏的还是干净的,echo 都会返回退出代码0,因为会有一些字符串要echo编辑。

回答by kimamula

git diff --quiet && git diff --staged --quiet || git commit -am 'Added license headers'

This command do exactly what is required, 'git commit only if there are changes', while the commands in the other answers do not: they only ignore any error of git commit.

这个命令完全符合要求,'git commit only if there are changes',而其他答案中的命令则没有:它们只忽略 .gitignore 的任何错误git commit

回答by marekventur

You can also just catch the error code with an OR operator:

您也可以使用 OR 运算符捕获错误代码:

git commit -m "Added license headers" || echo "No changes to commit"