[英]Jenkins pipeline step happens on master instead of slave
I am getting started with Jenkins Pipeline.我开始使用 Jenkins 管道。 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.我的问题是它在 master 上运行。
They are both Windows machines.它们都是 Windows 机器。
Here's my Jenkinsfile
:这是我的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
. pwd()
打印C:\Jenkins\workspace\<pipeline-name>_<branch-name>-Q762JIVOIJUFQ7LFSVKZOY5LVEW5D3TLHZX3UDJU5FWYJSNVGV4Q
。
This folder is on master.这个文件夹在 master 上。 This is confirmed by the presence of the test.txt
file. test.txt
文件的存在证实了这一点。
I expected test.txt
to be created on the slave agent.我希望在从属代理上创建test.txt
。
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.但是这个文件夹在MyAgent上不存在,这似乎与问题有关。
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.这个问题类似于Jenkins pipeline not honoring agent specification ,只是我没有使用build
指令,所以我认为答案不适用。
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
.这将打印预期的 output- master
和MyAgent
。 If this is correct, then why is the workspace located in a different folder on master instead of being on MyAgent?如果这是正确的,那么为什么工作区位于 master 上的不同文件夹中而不是在 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), 我遇到了类似的问题,并且以下管道代码对我有用(即,该文件是在Windows从属而不是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.因此即使echo
在代理上运行,但pwd()
将在内置节点上运行。 You can do sh 'pwd'
to get the path on the agent.您可以执行sh 'pwd'
以获取代理上的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.