如何将詹金斯管道阶段执行定向到从参数派生的特定节点代理? [英] 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}"
}
}
}
}
虽然初始化"阶段将全局变量 BuildAgentLabel 设置为不同的值(如果 NEW_LABEL 参数是长度 > 0 的字符串),构建"阶段总是在任何节点上执行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.
我希望初始化"阶段能够影响执行构建"阶段的节点,但似乎无法使其工作.怎样才能得到我想要的结果?
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 解析为它的值可能是由于这个错误 - Use this node as much as possible
并且默认为这个代理.
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}" }
这篇关于如何将詹金斯管道阶段执行定向到从参数派生的特定节点代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!