<p>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 <code>git commit -a -m 'Changes pushed by Jenkins' || true</code>, but better solutions would parse the output of git-add and only run git-commit when there is something to commit.</p>

CC BY-SA 4.0
2017-06-12T17:24:34.247Z
2019-05-29T03:12:48.190Z
2019-05-29T03:12:48.190Z
Jesusaur
<p>I'm going to assume that you have a way to know if the commit is empty or not.</p>

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

<pre><code>if( some use case ) {
    currentBuild.result = 'SUCCESS'
    echo "Job succeeded."
    return
}
</code></pre>

<p>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.</p>

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

CC BY-SA 3.0
2017-06-12T06:28:09.967Z
Hi @Alex, Thanks for your answer. I have updated my question, can you check my updated question.
Buvanesh Kumar
CC BY-SA 3.0
2017-06-12T13:39:35.920Z
@BuvaneshKumar Have you done any googling? It looks like there's plenty of ways to allow empty commits from git's side. https://stackoverflow.com/questions/22040113/how-to-let-jenkins-git-commit-only-if-there-are-changes
Alex
CC BY-SA 3.0
2017-06-09T12:57:26.737Z
2017-06-09T12:57:26.737Z
Alex
<p>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: </p>

<pre><code>git add -A
git diff-index --quiet HEAD || git commit -m "Jenkins automatic update commit"
</code></pre>

<ul>
<li>the first will eventually add all unstaged files in the whole working tree (equivalent to <code>git add --all</code>);</li>
<li>the second will perform the commit if and only if there are differences against the HEAD version of the working directory. If the <code>git diff</code> command has exit code zero then the <code>git commit</code> command is executed.</li>
</ul>

CC BY-SA 4.0
2022-11-18T09:08:07.813Z
If the git diff command has exit code 1, then the git commit command is executed. https://git-scm.com/docs/git-diff-index#Documentation/git-diff-index.txt---exit-code
Meteor
CC BY-SA 4.0
2018-11-15T17:21:17.133Z
Francesco: can you explain how this works also?
chicks
CC BY-SA 4.0
2018-11-16T10:59:57.683Z
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
CC BY-SA 4.0
2018-11-14T13:23:04.627Z
2018-11-16T16:03:13.923Z
2018-11-16T16:03:13.923Z
Francesco Fornari
<p>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 &amp; 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?</p>

<p><strong>UPDATE:</strong> 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).</p>

<pre><code>[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
</code></pre>

<jenkins><git><jenkins-pipeline><gitlab><jenkins2>
Jenkins shows the job as failed if there is nothing to commit to gitlab
Buvanesh Kumar
# Jenkins shows the job as failed if there is nothing to commit to gitlab

(Asked by: username_0 on 2017-06-08)

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

**Tags:** jenkins, git, jenkins-pipeline, gitlab, jenkins2


---

## 3 answers

### Answer by username_1 on 2017-06-12. Score: 6 | Accepted answer

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.


---

### Answer by username_3 on 2018-11-14. Score: 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.

> **Comments:**
> **username_5**: Francesco: can you explain how this works also?  
> **username_3**: 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?  
> **username_4**: If the git diff command has exit code 1, then the git commit command is executed. https://git-scm.com/docs/git-diff-index#Documentation/git-diff-index.txt---exit-code  

---

### Answer by username_2 on 2017-06-09. Score: 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".

> **Comments:**
> **username_0**: Hi @username_2, Thanks for your answer. I have updated my question, can you check my updated question.  
> **username_2**: @BuvaneshKumar Have you done any googling? It looks like there's plenty of ways to allow empty commits from git's side. https://stackoverflow.com/questions/22040113/how-to-let-jenkins-git-commit-only-if-there-are-changes  

---

