栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在声明性詹金斯管道中-我可以动态设置代理标签吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在声明性詹金斯管道中-我可以动态设置代理标签吗?

要查看其工作原理,请使用

GString
对象执行a
println
并同时返回agentName的变量。您可以从输出中看到,该行在任何其他管道代码之前的评估效果都很好。

agentName = "Windows"agentLabel = "${println 'Right Now the Agent Name is ' + agentName; return agentName}"pipeline {    agent none    stages {        stage('Prep') { steps {     script {         agentName = "Linux"     } }        }        stage('Checking') { steps {     script {         println agentLabel         println agentName     } }        }        stage('Final') { agent { label agentLabel } steps {     script {         println agentLabel         println agentName     } }    }    }}

控制台输出(请注意,在此实例上我实际上没有节点标记为Windows,因此在找不到该节点后中止了操作):

Started by user Admin[Pipeline] echoRight Now the Agent Name is Windows[Pipeline] stage[Pipeline] { (Prep)[Pipeline] script[Pipeline] {[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Checking)[Pipeline] script[Pipeline] {[Pipeline] echoWindows[Pipeline] echoLinux[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Final)[Pipeline] nodeStill waiting to schedule taskThere are no nodes with the label ‘Windows’Aborted by Admin[Pipeline] // node[Pipeline] }[Pipeline] // stage[Pipeline] End of PipelineERROR: Queue task was cancelledFinished: ABORTED

请注意,该行在

Right Now the Agent Name isWindows
输出中很早就出现了。这说明了为什么您的值为空。在您的脚本修改变量之前很久就对该语句进行了评估。

我可能稍后会尝试使用惰性

GString
来获取变量。

agentLabel = "${-> println 'Right Now the Agent Name is ' + agentName; return agentName}"

不幸的是,这会引发错误,因为它期望使用String类型。显然,它可以自行将非惰性GString强制转换为String,而不是强制性版本。因此,当我强制强制使用String时,它会在那时(也就是在管道代码实际运行之前)对变量进行求值。

agent { label agentLabel as String }

您可以通过使用旧的节点分配方法来解决此问题:

agentName = "Windows"agentLabel = "${-> println 'Right Now the Agent Name is ' + agentName; return agentName}"pipeline {    agent none    stages {        stage('Prep') { steps {     script {         agentName = "Linux"     } }        }        stage('Checking') { steps {     script {         println agentLabel         println agentName     } }        }        stage('Final') { steps {     node( agentLabel as String ) {  // evaluate the node label later         echo "TEST"     }     script {         println agentLabel         println agentName     } }        }    }}

您可以从此控制台输出中看到,它现在可以正确找到Linux节点并完成管道。Windows永远不会发生agentName == Windows的早期评估:

Started by user Admin[Pipeline] stage[Pipeline] { (Prep)[Pipeline] script[Pipeline] {[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Checking)[Pipeline] script[Pipeline] {[Pipeline] echoRight Now the Agent Name is Linux[Pipeline] echoLinux[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Final)[Pipeline] echoRight Now the Agent Name is Linux[Pipeline] nodeRunning on Slave 1 in /home/jenkinsslave/jenkins/workspace/test[Pipeline] {[Pipeline] echoTEST[Pipeline] }[Pipeline] // node[Pipeline] script[Pipeline] {[Pipeline] echoRight Now the Agent Name is Linux[Pipeline] echoLinux[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] End of PipelineFinished: SUCCESS

如果没有延迟

GString
和类型强制,这可能会起作用,但是我没有尝试过。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号