Issue
What do I have: job with some parallel stages:
pipeline {
agent none
stages {
stage('Run Tests') {
parallel {
stage('Test 1') {
agent {
label "agent1"
}
steps {
sh "echo 1"
}
}
stage('Test 2') {
agent {
label "agent1"
}
steps {
sh "echo 1"
}
}
}
}
}
}
I want to run parallel stages on different nodes: if first stage took an executor, second stage should be executed on another fresh node with label "agent1".
I provisioned single node with the label "agent1", both stages were executed on the same node.
When I provisioned one more node, stages were executed on different nodes. How should I change the pipeline to tell Jenkins to run each stage on separate node? Let's say, I will add two more stages, and I want to run them on four separate physical nodes (EC2 in my case).
Node configuration "Number of executors" is set to 1, "Minimum number of instances" is set to 0, "Minimum number of spare instances" is set to 0, "Instance cap" is empty.
Solution
Assuming you are using the Amazon EC2 Plugin there is a nice option that you can configure for each AMI called Maximum Total Uses:
Will set a maximum total uses for slaves launched from this template. After running that amount of jobs, the slave will be terminated. Use '-1' for unlimited slave uses.
This means that you can set this value to 1 and the plugin will automatically dispose the agent after the first job (or stage in your case as the agent
directive is per stage), and lunch a new one for the next job.
This is a great embedded mechanism for guaranteeing that jobs (or stages) are always running on a new fresh agent for each execution.
Answered By - Noam Helmer