Jenkins pipeline step happens on master instead of slave
17:56 13 Mar 2018

I am getting started with Jenkins Pipeline. My pipeline has one simple step that is supposed to run on a different agent - like the "Restrict where this project can be run" option.

My problem is that it is running on master.

They are both Windows machines.

Here's my Jenkinsfile:

pipeline {
  agent {label 'myLabel'}
  stages {
    stage('Stage 1') {
      steps {
        echo pwd()
        writeFile(file: 'test.txt', text: 'Hello, World!')
      }
    }
  }
}

pwd() prints C:\Jenkins\workspace\_-Q762JIVOIJUFQ7LFSVKZOY5LVEW5D3TLHZX3UDJU5FWYJSNVGV4Q.

This folder is on master. This is confirmed by the presence of the test.txt file.

I expected test.txt to be created on the slave agent.

Note 1

I can confirm that the pipeline finds the agent because the logs contain:

[Pipeline] node
Running on MyAgent in C:\Jenkins\workspace\_-Q762JIVOIJUFQ7LFSVKZOY5LVEW5D3TLHZX3UDJU5FWYJSNVGV4Q

But this folder does not exist on MyAgent, which seems related to the problem.

Note 2

This question is similar to Jenkins pipeline not honoring agent specification , except that I'm not using the build instruction so I don't think the answer applies.

Note 3

pipeline {
  agent any
  stages {
    stage('Stage 1') {
      steps {
        echo "${env.NODE_NAME}"
      }
    }
    stage('Stage 2') {
      agent {label 'MyLabel'}
      steps {
          echo "${env.NODE_NAME}"
      }
    }
  }
}

This prints the expected output - master and MyAgent. If this is correct, then why is the workspace located in a different folder on master instead of being on MyAgent?

jenkins continuous-integration jenkins-pipeline