title: Scripted Jenkinsfile executes scheduling on wrong nodes
body: <h1>The system/setup</h1>
  <p>Here is my setup:</p>
  <ol>
  <li>1 machine running Jenkins which also provides the <code>built-in</code> node (former <code>master</code>). It has 4 workers assigned to it</li>
  <li>1 FreeBSD machine as remote SSH node with the name <code>FreeBSD 13.0 x86_64</code>. It has 1 worker</li>
  <li>1 Ubuntu machine as remote SSH node with the name <code>Ubuntu 20.04 x86_64</code>. It has 1 worker</li>
  </ol>
  <p>I would like to have a sequence of <code>Checkout</code>, <code>Build</code>, <code>Test</code> and <code>Upload</code> steps executed serially on each of those <code>FreeBSD 13.0 x86_64</code> and <code>Ubuntu 20.04 x86_64</code> nodes. However, I would like each of those nodes to work independently on their list of serial tasks. Something like this:</p>
  <p><a href="https://i.stack.imgur.com/arG9V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/arG9V.png" alt="enter image description here" /></a></p>
  <p>For this, I have created the following scripted Jenkinsfile</p>
  <pre><code>String[] nodesNames = [
      'FreeBSD 13.0 x86_64',
      'Ubuntu 20.04 x86_64'
  ]
  
  Map builders = [:]
  for (nodeName in nodesNames) {
      builders[nodeName] = {
          node(nodeName) {
              stage('Checkout') {
                  sh 'sleep 5'
              }
              stage('Build') {
                  sh 'sleep 10'
              }
              stage('Test') {
                  sh 'sleep 15'
              }
              stage('Upload') {
                  sh 'sleep 20'
              }
          }
      }
  }
  
  parallel builders
  </code></pre>
  <h1>The problem</h1>
  <p>Jenkins will need an executor to execute the Jenkinsfile itself, which is scheduling the real work. Again, not the work, but the scheduling of the work. And it picks one up from the nodes like this:</p>
  <pre><code>Running on Ubuntu 20.04 x86_64 in /home/jenkins/workspace/test
  </code></pre>
  <p>That is the problem: it picks up the wrong node. That node has one executor, and it should never run the scheduling. Scheduling should be done on the <code>built-in</code> node. This will later result in the following message:</p>
  <pre><code>Still waiting to schedule task
  Waiting for next available executor on ‘Ubuntu 20.04 x86_64’
  </code></pre>
  <h1>The outcome</h1>
  <p>Because one of the 1-worker-node is doing both scheduling and actual work, we will end up with a seemengly &quot;parallel&quot; execution but in fact, is as serial as it can be. Here is a picture taken in the middle of the whole process. Notice how the FreeBSD machine is alternatively doing work then scheduling. As it happens, is giving work to itself. When the work for itself is finished, it will start giving work to Ubuntu.</p>
  <p><a href="https://i.stack.imgur.com/X9jBo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X9jBo.png" alt="enter image description here" /></a></p>
  <h1>The solution?</h1>
  <p>How can one tell Jenkins to execute the Jenkins file itself (the scheduling part) on the <code>built-in</code> node (the former <code>master</code>) and not use a precious worker from the actual remote nodes?</p>
  <h1>The non-maintainable solution</h1>
  <p>(Update after initial question and as a response for @MaratC)</p>
  <p>We can use declarative syntax. However, it has a major/crippling flaw: imagine one needs to add another machine. It will basically repeat a lot of code. After the 4th-5th machine, it becomes unmaintainable.</p>
  <pre><code>pipeline {
      agent { node('built-in') }
      stages {
          stage('Build all') {
              parallel {
                  stage('FreeBSD') {
                      agent { node('FreeBSD 13.0 x86_64') }
                      stages {
                          stage('Checkout') {
                              steps { sh 'sleep 5' }
                          }
                          stage('Build') {
                              steps { sh 'sleep 10' }
                          }
                          stage('Test') {
                              steps { sh 'sleep 15' }
                          }
                          stage('Upload') {
                              steps { sh 'sleep 20' }
                          }
                      }
                  }
                  stage('Ubuntu') {
                      agent { node('Ubuntu 20.04 x86_64') }
                      stages {
                          stage('Checkout') {
                              steps { sh 'sleep 5' }
                          }
                          stage('Build') {
                              steps { sh 'sleep 10' }
                          }
                          stage('Test') {
                              steps { sh 'sleep 15' }
                          }
                          stage('Upload') {
                              steps { sh 'sleep 20' }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>