1

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
2

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.

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

  • Hi @Alex, Thanks for your answer. I have updated my question, can you check my updated question. – Buvanesh Kumar Jun 12 '17 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 Jun 12 '17 at 13:39
1

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.
  • Francesco: can you explain how this works also? – chicks Nov 15 '18 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? – Francesco Fornari Nov 16 '18 at 10:59
  • Thanks, that is much clearer. – chicks Nov 16 '18 at 12:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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