如何将jenkins管道阶段执行定向到从参数派生的特定节点代理? [英] How to direct jenkins pipeline stage execution to a particular node agent derived from parameter?
问题描述
我有多种Jenkins节点代理,包括"master","tiering_agent1"和"cirrus".我正在尝试通过参数{}设置来设置执行舞台的节点.
I have multipe Jenkins node agents, including "master", "tiering_agent1", and "cirrus". I am trying to set the node on which a Stage is executed by a parameters{} setting.
我有这个管道代码
def BuildAgentLabel='tiering_agent1'
pipeline {
agent { label 'master' }
parameters {
string(
name: 'NEW_LABEL',
defaultValue: '',
description: ''
)
}
stages {
stage( 'Init') {
steps {
script {
if ( params.NEW_LABEL != '' ){
echo "Setting BuildAgentLabel to '${params.NEW_LABEL}'"
BuildAgentLabel = params.NEW_LABEL
echo "BuildAgentLabel is now '${BuildAgentLabel}'"
}
}
}
}
stage( "Build") {
agent { label BuildAgentLabel }
steps {
echo "Performing Stage '${STAGE_NAME}' on NODE '${env.NODE_NAME}'"
echo "BuildAgentLabel=${BuildAgentLabel}"
}
}
}
}
尽管'Init'阶段将全局变量 BuildAgentLabel 设置为其他值(如果NEW_LABEL参数是长度大于0的字符串),则'Build'阶段始终在任何节点上执行"def BuildAgentLabel"语句最初设置为.
Though the 'Init' stage sets the global variable BuildAgentLabel to a different value( if the NEW_LABEL parameters is a string with length > 0), the 'Build' stage always gets executed on whatever node the "def BuildAgentLabel" statement is originally set to.
运行的控制台输出与此相呼应:
The console output of the run echoes this:
[Pipeline] node
Running on Jenkins in ...
[Pipeline] {
.
.
.
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Setting BuildAgentLabel to 'master'
[Pipeline] echo
BuildAgentLabel is now 'master'
.
.
.
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] node
Running on tiering_agent1 in /opt/jenkins-agent/workspace/ine-multibranch-test_master-RGJIAQXOIAPL7XDIJW6DOGF4KUE5KBRXCAZ7U4IUW2YOTZVQTWCA
[Pipeline] {
.
.
.
[Pipeline] {
[Pipeline] echo
Performing Stage 'Build' on NODE 'tiering_agent1'
[Pipeline] echo
BuildAgentLabel=master
.
.
.
[Pipeline] End of Pipeline
Finished: SUCCESS
这就像stage {}对象几乎是同时实例化并在那时捕获其代理标签一样,但是阶段执行是在此之后执行的.
It's like the stage{} objects are instantiated almost concurrently and snag their agent label at that time, but stage execution comes after that.
我希望'Init'阶段能够影响在其上执行'Build'阶段的节点,但似乎无法使其工作.如何获得想要的结果?
I would like the 'Init' stage to be able to affect the node on which the 'Build' stage is performed upon, but cannot seem to make it work. How can I get the result I want?
推荐答案
问题是agent { label BuildAgentLabel }
不能将变量BuildAgentLabel解析为其值,原因可能是此bug-
The issue is agent { label BuildAgentLabel }
doesn't resolve the variable BuildAgentLabel to it's value possibly due to this bug - https://issues.jenkins-ci.org/browse/JENKINS-9665 and I'm guessing your node agent 'tiering_agent1' is configured as Use this node as much as possible
and it's defaulting to this agent.
但是,直接将标签设置为参数agent { label "${params.NEW_LABEL}" }
However, setting label to the parameter directly works agent { label "${params.NEW_LABEL}" }
这篇关于如何将jenkins管道阶段执行定向到从参数派生的特定节点代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!