<p>First of all, a job should always be green on the first run in my opinion. I know that it can be very difficult, but every issue should be solved. That having said, we have also some jobs that get green after 5 retries. It are selenium tests that are very brittle and we do not get the time to fix them. Once a <code>retry</code> was added the builds are green for a couple of months now:</p>

<p><a href="https://jenkins.io/doc/book/pipeline/syntax/" rel="nofollow noreferrer">https://jenkins.io/doc/book/pipeline/syntax/</a></p>

<blockquote>
  <p>retry</p>

<pre><code>On failure, retry the entire Pipeline the specified number of times. For
example: options { retry(3) }
</code></pre>
</blockquote>

<p>I prefer a <code>retry</code> as it will only restart the job if there is a failure over <code>cron</code> as it does not check previous failures.</p>

CC BY-SA 4.0
2019-08-23T07:10:48.370Z
2019-08-23T07:10:48.370Z
030
<p>You can use <a href="https://wiki.jenkins.io/display/JENKINS/Naginator+Plugin" rel="nofollow noreferrer">Naginator plugin</a> in Jenkins to retry the build. This plugin has multiple options. </p>

<p>Step 1: Install the Naginator plugin via the Plugin Manager.</p>

<p>Step 2: In the post-build action of the job, the new option appears as <code>Retry after the build failure</code></p>

<p><a href="https://i.sstatic.net/H6c2N.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/H6c2N.png" alt="enter image description here"></a> </p>

<p>Step 3: Configure <code>Retry after the build failure</code> action. Example below
<a href="https://i.sstatic.net/NYUXX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NYUXX.png" alt="enter image description here"></a></p>

<p>Step 4: If a job fails, you can identify whether successfully triggered or not it the build history 
<a href="https://i.sstatic.net/6OpG8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6OpG8.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2019-08-26T05:29:59.633Z
2020-01-05T01:57:10.850Z
2020-01-05T01:57:10.850Z
Siva Kumar Reddy G
<p>This pipeline will run each day at 1 AM. It'll restart if the build fails.</p>

<pre class="lang-java prettyprint-override"><code>pipeline {
    agent any

    triggers {
        cron('0 1 * * *')
    }

    stages {
        stage ('Build') {
            when {
                expression {
                    // When last build has failed
                    !hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) == true
                }
            }

            steps {
                sh "./myjob.sh"
            }

            post {
                failure {
                    // If the current job has failed, trigger it without
                    // waiting.
                    build job: "${JOB_NAME}", wait: false
                }
            }
        }
    }
}
</code></pre>

<p>If you want to wait before to re-trigger the job, you can add the option <code>quietPeriod: (seconds)</code> to the <code>build</code> step (<a href="https://jenkins.io/doc/pipeline/steps/pipeline-build-step/" rel="noreferrer">https://jenkins.io/doc/pipeline/steps/pipeline-build-step/</a>).</p>

<p>You might have to add these methods to the scriptApproval:</p>

<p><a href="https://i.sstatic.net/Wi1Ke.png" rel="noreferrer"><img src="https://i.sstatic.net/Wi1Ke.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2019-08-22T08:30:21.703Z
2019-08-22T12:53:59.227Z
2019-08-22T12:53:59.227Z
Hedi Nasr
<p>We have got test automation in Jenkins, and we would like to execute it at midnight.</p>

<p>But sometimes it fails, and sometimes it passes. Our idea is to execute the daily builds from 1:00 AM until it is green. How we can execute a job until it is green?</p>

<p>For example:</p>

<ul>
<li>Execute job at 1:00</li>
<li>Execute job at 2:00 if the previous is green, etc.</li>
</ul>

<p>We can run it hourly, but how is it possible to insert the condition?</p>

<jenkins><builds>
How to execute a Jenkins job until the build is green
Tibor Száraz
# How to execute a Jenkins job until the build is green

(Asked by: username_0 on 2019-08-22)

We have got test automation in Jenkins, and we would like to execute it at midnight.

But sometimes it fails, and sometimes it passes. Our idea is to execute the daily builds from 1:00 AM until it is green. How we can execute a job until it is green?

For example:

* Execute job at 1:00
* Execute job at 2:00 if the previous is green, etc.

We can run it hourly, but how is it possible to insert the condition?

**Tags:** jenkins, builds


---

## 3 answers

### Answer by username_3 on 2019-08-22. Score: 6 | Accepted answer

This pipeline will run each day at 1 AM. It'll restart if the build fails.
pipeline {
    agent any

    triggers {
        cron('0 1 * * *')
    }

    stages {
        stage ('Build') {
            when {
                expression {
                    // When last build has failed
                    !hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) == true
                }
            }

            steps {
                sh "./myjob.sh"
            }

            post {
                failure {
                    // If the current job has failed, trigger it without
                    // waiting.
                    build job: "${JOB_NAME}", wait: false
                }
            }
        }
    }
}

If you want to wait before to re-trigger the job, you can add the option quietPeriod: (seconds) to the build step (https://jenkins.io/doc/pipeline/steps/pipeline-build-step/).
You might have to add these methods to the scriptApproval:


---

### Answer by username_1 on 2019-08-23. Score: 2

First of all, a job should always be green on the first run in my opinion. I know that it can be very difficult, but every issue should be solved. That having said, we have also some jobs that get green after 5 retries. It are selenium tests that are very brittle and we do not get the time to fix them. Once a retry was added the builds are green for a couple of months now:
https://jenkins.io/doc/book/pipeline/syntax/

retry
On failure, retry the entire Pipeline the specified number of times. For
example: options { retry(3) }


I prefer a retry as it will only restart the job if there is a failure over cron as it does not check previous failures.


---

### Answer by username_2 on 2019-08-26. Score: 2

You can use [Naginator plugin](https://wiki.jenkins.io/display/JENKINS/Naginator+Plugin) in Jenkins to retry the build. This plugin has multiple options.

Step 1: Install the Naginator plugin via the Plugin Manager.

Step 2: In the post-build action of the job, the new option appears as `Retry after the build failure`

[![enter image description here](https://i.sstatic.net/H6c2N.png)](https://i.sstatic.net/H6c2N.png)

Step 3: Configure `Retry after the build failure` action. Example below
[![enter image description here](https://i.sstatic.net/NYUXX.png)](https://i.sstatic.net/NYUXX.png)

Step 4: If a job fails, you can identify whether successfully triggered or not it the build history
[![enter image description here](https://i.sstatic.net/6OpG8.png)](https://i.sstatic.net/6OpG8.png)


---

