How to let Jenkins git commit only if there is a change?

I have a Jenkins job that builds from the leading branch of the github.com repository with Maven ( mvn clean install), then checks the license headers in Java files and skips NOTICE files and adds them ( mvn license:format notice:generate) if necessary . Sometimes this leads to the modification or addition of files, sometimes not.

Whenever any changes have been made (license plugin), I want to push the changes to the github registry.

Now it’s hard for me to figure out how best to achieve this. I added the shell build step after the Maven license plugin, where I execute the git commands:

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

git add .works, i.e. does not break the assembly, even if no files are added. However, it git commitbreaks the assembly if there are no changes at all.

I'm not worried about returning to github, as I believe that the post-build git action for publishers can do this for me. Can someone point me in the right direction for git commit?

+4
source share
4 answers

To stop the build from breaking at the build step of the shell, returning the completion code 1at any point, for example, when you try to execute the git command, although you do nothing, you can simply wrap the corresponding commands in an echo.

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

Now, are there any unnecessary files that need to be added to the index or not, and whether the working tree is dirty or clean, echo will return the exit code 0, as there will be some line that will be echoed.

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

, , "git commit, ", : git commit.

+13

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

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

Better to use

  git commit --allow-empty -a -m "Added license headers"
0
source

Source: https://habr.com/ru/post/1528946/


All Articles