HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to execute a Jenkins job until the build is green

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
theuntiljenkinsbuildhowgreenjobexecute

Problem

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?

Solution

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:

Context

StackExchange DevOps Q#8958, answer score: 6

Revisions (0)

No revisions yet.