When working with Jenkins pipelines, you will encounter instances where you need to pause the execution of a specific build for a given duration.
There are various reasons why you need to pause a Jenkins job: you can pause a Jenkins job to wait for external dependencies. For instance, if your build depends on external resources or processes that are not yet available, you may need to pause until the required dependencies are ready.
Another reason is resource distribution. If you have limited resources available, such as build agents or test environments, you may need to pause builds to ensure that those resources are used efficiently.
In this tutorial, we will learn how to use the sleep feature in Jenkinsfile to allow a Jenkins pipeline to pause for a given duration.
Jenkinsfile Sleep
In a Jenkinsfile, we can introduce a sleep block that specifies the sleep information for a given build. This block allows us to define the duration and the unit that a given job will sleep. It is typically used to introduce a delay in the build process for one of the reasons mentioned above, such as allowing time for external dependencies to become available or distributing resources.
The syntax of the sleep block in Jenkins is as shown:
The time section allows you to specify the length of time for which the build will sleep. This is a non-optional integer value.
On the other hand, the unit section specifies the time parameter for the build. Accepted values include NANOSECOND, MICROSECOND, MILLISECONDS, SECONDS, MINUTES, HOURS, and DAYS.
This is an optional parameter and Jenkins will default to SECONDS if not specified.
Example Usage
The following shows a simple Jenkinsfile example:
agent any
stages {
stage('Start') {
steps {
sh 'date +%s'
}
}
stage('Pausing') {
steps {
sleep(time: 2, unit: 'MINUTES')
}
}
stage('End') {
steps {
sh "date +%s"
}
}
}
}
The Pipeline above should pause the build for 2 minutes.
Once you run the build above, you should get the output as shown:
[Pipeline] node
Running on 172.27.247.69 in /home/debian/jenkins/workspace/slepp_demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Start)
[Pipeline] sh
+ date +%s
1672556534
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Pausing)
[Pipeline] sleep
Sleeping for 2 min 0 sec
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (End)
[Pipeline] sh
+ date +%s
1672556654
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
Jenkins Pause in Unix Systems
If you do not wish to use Jenkins sleep block, you can pause a job for a given duration using the sleep command in Unix systems.
The syntax is as shown:
where duration denotes the number of seconds that the script should pause, this must be an integer value or a variable holding an integer value.
In a Jenkinsfile, you can invoke the sleep command using the sh command:
Conclusion
In this article, you learned how to use the sleep block in a Jenkinsfile to pause the execution of a given build for a specified duration. The block enables us to define the time interval and the unit of time.