HiveBrain v1.2.0
Get Started
← Back to all entries
patterngitModerate

Jenkins shows the job as failed if there is nothing to commit to gitlab

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
committhejenkinsgitlabshowsnothingfailedtherejob

Problem

I have been using one Jenkins job, that will pull repository from gitlab.
Then it will modify the files and push it into gitlab (only if any user added any new files/modify contents in gitlab). Otherwise, it simply does empty commit.
When empty commits & push happens, Jenkins job shows me like the job is failed. For only this case, I want to make Jenkins should show as successful.
Is there any way to do it?

UPDATE: Here is the log I got from the job. I am expecting SUCCESS even if the Jenkins git pushes the empty repository (as shown as the below).

[1. Extract simplified configuration] $ /bin/sh -xe 
/tmp/jenkins2112147489595430303.sh
+ chmod +x script.sh
+ ./script.sh
+ git add .
+ git commit -a -m 'Changes pushed by Jenkins'
HEAD detached at ee35229
nothing to commit, working directory clean
Build step 'Execute shell' marked build as failure
Triggering a new build of 2. Erase Answers
Finished: FAILURE

Solution

In my automatic jenkins job, launched daily, if there are no changes the git commit command returns 1. That will mark the build as failed. To solve this problem I use these two commands in my shell build step:

git add -A
git diff-index --quiet HEAD || git commit -m "Jenkins automatic update commit"


  • the first will eventually add all unstaged files in the whole working tree (equivalent to git add --all);



  • the second will perform the commit if and only if there are differences against the HEAD version of the working directory. If the git diff command has exit code zero then the git commit command is executed.

Code Snippets

git add -A
git diff-index --quiet HEAD || git commit -m "Jenkins automatic update commit"

Context

StackExchange DevOps Q#1325, answer score: 11

Revisions (0)

No revisions yet.