简体   繁体   中英

Jenkins pipeline step happens on master instead of slave

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\<pipeline-name>_<branch-name>-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\<pipeline-name>_<branch-name>-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?

here is an example

pipeline {
  agent none

    stages {
        stage('Example Build') {
            agent { label 'build-label' }
            steps {
                sh 'env'
                sh ' sleep 8'
            }
        }
        stage('Example Test') {
            agent { label 'deploy-label' }
            steps {
                sh 'env'
                sh ' sleep 5'
            }
        }
    }
}

I faced similar issue and the following pipeline code worked for me (ie the file got created on the Windows slave instead of Windows master),

pipeline {
    agent none
    stages {
        stage("Stage 1") {
            steps {
                node('myLabel'){
                    script {
                        writeFile(file: 'test.txt', text: 'Hello World!', encoding: 'UTF-8')
                    }
                    // This should print the file content on slave (Hello World!)
                    bat "type test.txt"
                }
            }
        }
    }
}

I'm debugging a completely unrelated issue and this fact was thrown in my face. Apparently the pipeline is processed in the built-in node (previously known as the master node), with the steps being forwarded to the agent.

So even though echo runs on the agent, but pwd() will run on the built-in node. You can do sh 'pwd' to get the path on the agent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM