If you have a Jenkins declarative pipeline , you’re generally bound to have more than one stage with steps within each of them. The usual way of declaring a node/agent/slave is by declaring an agent directive encompassing the stages{} directive, like so:
pipeline {
agent { label 'mynode' }
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}
However, occasionally you may wish to run a stage or two that doesn’t require an agent. A simple example would be a timeout
or a sleep
stage that is waiting for a previous stage to finish. If your timeout lasts more than a few minutes, you’d want to release the agent so another build may use it. Holding up an agent is a crime in Jenkins world.
(Although this is shown in the document link I shared above, it isn’t tagged for this usecase as such.)
Here’s a simple way to go about doing that:
pipeline {
agent none
stages {
stage('Stage Do Something') {
agent { label 'mynode' }
steps {
echo "Something"
}
}
stage('Stage Sleep') {
agent none
steps {
sleep time: 3, unit: 'HOURS'
}
}
stage('Stage Do More Of Something') {
agent { label 'mynode' }
steps {
echo "More Of Something"
}
}
}
}
‘Stage Sleep’ here is declared with agent none
which simply retires the agent until it is called again in the next stage.
Hope that helps someone looking for a quick answer.