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.
Pipelines are made up of multiple steps that allow you to build, test and deploy applications. Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action.
Yes you can only if you want to have external function inside step block.
Since version 1.3 of Declarative Pipeline plugin, this is officially supported. It's officially called "Sequential Stages".
pipeline { agent none stages { stage("check code style") { agent { docker "code-style-check-image" } steps { sh "./check-code-style.sh" } } stage("build and test the project") { agent { docker "build-tools-image" } stages { stage("build") { steps { sh "./build.sh" } } stage("test") { steps { sh "./test.sh" } } } } } }
Official announcement here: https://jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With