如何使用groovy在jenkins中获取当前构建的节点名称 [英] How to get the current build's node name in jenkins using groovy
问题描述
我在Jenkins中运行一个管道作业,我想知道它正在运行的节点的名称.有没有办法从作业的Groovy脚本中获取节点名称?
I have a pipeline job running in Jenkins and I want to know the name of the node it's running on. Is there a way to get the node name from within the job's Groovy script?
我尝试了以下代码:
print currentBuild.getBuiltOn().getNodeName()
错误是:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method org.jenkinsci.plugins.workflow.job.WorkflowRun getBuiltOn
我也尝试过:
def build = currentBuild.build()
print build.getExecutor().getOwner().getNode().getNodeName()
但结果是".
推荐答案
环境变量'NODE_NAME'具有此变量.
There is an environment variable 'NODE_NAME' which has this.
您可以这样访问它:
echo "NODE_NAME = ${env.NODE_NAME}"
编辑管道作业时,可以通过转到管道语法"帮助链接(页面左侧)找到所有可用的环境变量,然后查找全局变量"部分,然后单击至全局"变量参考". "env"部分列出了可用的环境变量.
When you are editing a pipeline job, you can find all the available environment variables by going to the "pipeline syntax" help link (left of page) then look for the "Global Variables" section and click through to the "Global Variables Reference". There is a section "env" that lists the environment variables available.
这篇关于如何使用groovy在jenkins中获取当前构建的节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!