Goal
Run multiple stages of a declarative Jenkins pipeline on the same node.
Setup
This is just a minimal example to show the problem. There are 2 Windows nodes "windows-slave1" and "windows-slave2" both labeled with the label "windows".
NOTE: My real Jenkinsfile cannot use a global agent because there are groups of stages that require to run on different nodes (e.g. Windows vs. Linux).
Expected Behaviour
Jenkins selects one of the nodes in "Stage 1" based on the label and uses the same node in "Stage 2" because the variable windowsNode was updated to the node selected in "Stage 1".
Actual Behaviour
"Stage 2" sometimes runs on the same and sometimes on a different node than "Stage 1". See the output below.
Jenkinsfile
#!groovy
windowsNode = 'windows'
pipeline {
agent none
stages {
stage('Stage 1') {
agent {
label windowsNode
}
steps {
script {
// all subsequent steps should be run on the same windows node
windowsNode = NODE_NAME
}
echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
}
}
stage('Stage 2') {
agent {
label windowsNode
}
steps {
echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
}
}
}
}
Output
[Pipeline] stage
[Pipeline] { (Stage 1)
[Pipeline] node
Running on windows-slave2 in C:\Jenkins\workspace\test-agent-allocation@2
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
windowsNode: windows-slave2, NODE_NAME: windows-slave2
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Stage 2)
[Pipeline] node
Running on windows-slave1 in C:\Jenkins\workspace\test-agent-allocation
[Pipeline] {
[Pipeline] echo
windowsNode: windows-slave2, NODE_NAME: windows-slave1
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
Any ideas what's wrong with the setup? I guess it's how the Jenkinsfile is parsed and executed.
Other suggestions? Maybe there is a Jenkins API to select a node based on the "windows" label when setting windowsNode initially.
windowsNode = 'windows'
towindowsNode = 'asdf'
then you should see a "There are no nodes with the label ‘asdf’" error, meaning your stage 2 is still runninglabel 'windows'
and notlabel 'windows-slave2'
. – Quanticstage("Stage 2" ) { environment { someVariable = "$windowsNode" } agent { label env.someVariable } ..
and although it allocates a node and gives no "missing property" errors (meaningagent
at least is aware that someVariable exists), it does NOT work. If I print the value ofenv.someVariable
it is the node name, so although it can see the correctwindowsNode
variable, I suspect thatsomeVariable
is in some blank state in theagent
directive causing it to run something likeagent label ''
instead ofagent label 'windows-slave2'
. – Quanticenvironment { someVariable = "\'$windowsNode\'" }
which makes the variable'windows-slave2'
instead of justwindows-slave2
, but it still randomly allocates between my two test nodes (still no errors about the variable or label name though). – Quantic