Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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.

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 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 commit breaks the build, if there aren't any changes at all.

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?

like image 371
s.d Avatar asked Feb 26 '14 11:02

s.d


People also ask

How do I build on specific commit in git on Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.

How do you trigger a build only if changes happen on a particular set of files?

If you are using a declarative syntax of Jenkinsfile to describe your building pipeline, you can use changeset condition to limit stage execution only to the case when specific files are changed. This is now a standard feature of Jenkins and does not require any additional configruation/software.

Is it possible for SCM to check whether changes were made ie new commits and builds the project if new commits were pushed on Jenkins?

You can trigger build by following two methods: Poll SCM : periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build. build periodically : builds the project periodically even if nothing has changed.

Which command is used to do direct commit without moving changes into staging?

git add -u will stage everything for you in one command.


1 Answers

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.

like image 157
kimamula Avatar answered Oct 11 '22 21:10

kimamula