7

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

3 Answers 3

6

In order to get a success, you need to avoid non-zero exit codes. A simple solution would be to change the last line of your script to git commit -a -m 'Changes pushed by Jenkins' || true, but better solutions would parse the output of git-add and only run git-commit when there is something to commit.

0
11

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.
3
  • Francesco: can you explain how this works also?
    – chicks
    Commented Nov 15, 2018 at 17:21
  • In my automatic jenkins job, lauched daily, if there are no changes the git commit command returns 1. That will mark the buikd as failed. To solve this problem I use this two commands in my shell build step: - the first will eventually add all unstaged files in the whole working tree (equal 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 equals to zero the git commit command is executed. Is it clear now? Commented Nov 16, 2018 at 10:59
  • If the git diff command has exit code 1, then the git commit command is executed. git-scm.com/docs/git-diff-index#Documentation/…
    – Meteor
    Commented Nov 18, 2022 at 9:08
2

I'm going to assume that you have a way to know if the commit is empty or not.

You can use the following code to mark the job as a success instead of relying on error codes.

if( some use case ) {
    currentBuild.result = 'SUCCESS'
    echo "Job succeeded."
    return
}

You have two options for placement. Putting this inside a stage will exit only that stage as a success. Putting it outside a stage (but inside the node) will end the whole job as a success.

I can't be more specific without knowing your code, but I'd expect something like "make commit, if full continue as normal, if empty build result is success".

2
  • Hi @Alex, Thanks for your answer. I have updated my question, can you check my updated question. Commented Jun 12, 2017 at 6:28
  • @BuvaneshKumar Have you done any googling? It looks like there's plenty of ways to allow empty commits from git's side. stackoverflow.com/questions/22040113/…
    – Alex
    Commented Jun 12, 2017 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.