2

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?

6

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:

enter image description here

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.

1

You can use 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

Step 3: Configure Retry after the build failure action. Example below enter image description here

Step 4: If a job fails, you can identify whether successfully triggered or not it the build history enter image description here

2
  • Could you add a configuration snippet are some steps how to configure a retry using the plugin?
    – 030
    Dec 24 '19 at 12:24
  • 1
    @030, As requested added, updated the comments. Jan 5 '20 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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