There are some powerful steps that "wrap" other steps which can easily solve
problems like retrying (retry
) steps until successful or exiting if a
step takes too long (timeout
).
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
}
}
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
The "Deploy" stage retries the flakey-deploy.sh
script 3 times, and then
waits for up to 3 minutes for the health-check.sh
script to execute. If the
health check script does not complete in 3 minutes, the Pipeline will be marked
as having failed in the "Deploy" stage.
"Wrapper" steps such as timeout
and retry
may contain other steps,
including timeout
or retry
.
We can compose these steps together. For example, if we wanted to retry our
deployment 5 times, but never want to spend more than 3 minutes in total before
failing the stage:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh './flakey-deploy.sh'
}
}
}
}
}
}
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh './flakey-deploy.sh'
}
}
}
}