How to get the current build's node name in jenkins using groovy

我的梦境 提交于 2019-12-10 02:43:40

问题


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?

I have tried the below code:

print currentBuild.getBuiltOn().getNodeName()

the error is:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method org.jenkinsci.plugins.workflow.job.WorkflowRun getBuiltOn

I also tried this:

def build = currentBuild.build()
print build.getExecutor().getOwner().getNode().getNodeName()

but the result is ''.


回答1:


There is an environment variable 'NODE_NAME' which has this.

You can access it like this:

echo "NODE_NAME = ${env.NODE_NAME}"

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.



来源:https://stackoverflow.com/questions/44757172/how-to-get-the-current-builds-node-name-in-jenkins-using-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!