title: How to run multiple stages on the same node with declarative Jenkins pipeline?
body: <p><strong>Goal</strong><br>
  Run multiple stages of a declarative Jenkins pipeline on the same node.</p>
  
  <p><strong>Setup</strong><br>
  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".</p>
  
  <p>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).</p>
  
  <p><strong>Expected Behaviour</strong><br>
  Jenkins selects one of the nodes in "Stage 1" based on the label and uses the same node in "Stage 2" because the variable <em>windowsNode</em> was updated to the node selected in "Stage 1".</p>
  
  <p><strong>Actual Behaviour</strong><br>
  "Stage 2" sometimes runs on the same and sometimes on a different node than "Stage 1". See the output below.</p>
  
  <p><strong>Jenkinsfile</strong></p>
  
  <pre class="lang-groovy prettyprint-override"><code>#!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"
        }
      }
    }
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>[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
  </code></pre>
  
  <p>Any ideas what's wrong with the setup? I guess it's how the Jenkinsfile is parsed and executed.</p>
  
  <p>Other suggestions? Maybe there is a Jenkins API to select a node based on the "windows" label when setting <em>windowsNode</em> initially.</p>