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

12,135

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
12,135

Related videos on Youtube

Buvanesh Kumar
Author by

Buvanesh Kumar

Updated on September 18, 2022

Comments

  • Buvanesh Kumar
    Buvanesh Kumar over 1 year

    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
    
  • Buvanesh Kumar
    Buvanesh Kumar almost 7 years
    Hi @Alex, Thanks for your answer. I have updated my question, can you check my updated question.
  • Alex
    Alex almost 7 years
    @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/…
  • chicks
    chicks over 5 years
    Francesco: can you explain how this works also?
  • Francesco Fornari
    Francesco Fornari over 5 years
    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?