How to execute a Jenkins job until the build is green 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?
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: