简体   繁体   English

如果有变化,如何让Jenkins git提交?

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

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 ). 我有一个Jenkins工作,它是从github.com存储库的主分支和Maven( mvn clean install )构建的,然后检查Java文件中的许可证头并丢失NOTICE文件,并在必要时添加它们( mvn license:format notice:generate ) 。 Sometimes this will result in changed or added files, sometimes not. 有时这会导致更改或添加文件,有时不会。

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: 我在Maven许可插件之后添加了一个shell构建步骤,执行git命令:

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

git add . alone works, ie, doesn't break the build, even if no files have been added. 单独工作,即不会破坏构建,即使没有添加任何文件。 However, git commit breaks the build, if there aren't any changes at all. 但是,如果根本没有任何更改, 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. 我不担心回到github,因为我相信Git Publisher的后期构建操作可以为我做这件事。 Can someone please point me in the right direction for the git commit? 有人可以指点我正确的git提交方向吗?

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

这个命令完全按照要求执行,'git仅在有更改时提交',而其他答案中的命令则不执行:它们只忽略git commit任何错误。

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

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

To stop the build from breaking on the shell build step returning exit code 1 at any one point, eg, 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提交时,尽管没有任何提交,您可以简单地将相应的命令包装到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 echo ed. 现在,是否有未跟踪的文件要添加到索引,以及工作树是否脏或干净,echo将返回退出代码0 ,因为将有一些字符串要echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM