Allocating a node on the same agent after already allocating a node and using dir breaks PWD

2 minute readKnowledge base

Issue

In scripted Pipeline, if you allocate a node, then use the dir step to change directory, then allocate a new node on the same agent, the directory your build context will be in is incorrect.

If you have only one agent with the label docker, and it has at least two executor slots, you can reproduce this bug by running the following Pipeline:


node('docker') { stage('test') { sh 'pwd' dir('repo') { sh 'pwd' node('docker') { sh 'pwd' } } } } ---

Running this Pipeline will reproduce the issue during the third call to pwd. That call returns /docker/workspace/test/repo when it should be /docker/workspace/test@2:


Started by user admin [Pipeline] Start of Pipeline [Pipeline] node Running on docker in /docker/workspace/test [Pipeline] { [Pipeline] stage [Pipeline] { (test) [Pipeline] sh + pwd docker/workspace/test [Pipeline] dir Running in /docker/workspace/test/repo [Pipeline] { [Pipeline] sh + pwd /docker/workspace/test/repo [Pipeline] node Running on docker in /docker/workspace/test@2 [Pipeline] { [Pipeline] sh + pwd /docker/workspace/test/repo [Pipeline] } [Pipeline] // node [Pipeline] } [Pipeline] // dir [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS ---

Resolution

This issue was tracked by JENKINS-70528 and BEE-30693.

The fix was in: Pipeline: Basic Steps Version 1010.vf7a_b_98e847c1 Pipeline: Nodes and Processes Version 1234.v019404b_3832a

This fix is available in the CloudBees CI 2.387.1.2 release, tracked by BEE-30693 in the CloudBees CI release notes.

Workaround

Refactor your Pipeline code to not allocate a node when you are already on a node.

Tested product/plugin versions

CloudBees Jenkins Platform controller 2.346.1.4 CloudBees CI on modern cloud platforms - Managed controller 2.375.2.3 workflow-durable-task-step:1217.v38306d8fa_b_5c 'Pipeline: Nodes and Processes'