How to get the current assembly name of a node in jenkins using groovy

I have a pipeline job running in Jenkins and I want to know the name of the node being executed. Is there a way to get the node name from a Groovy script job?

I have tried the below code:

print currentBuild.getBuiltOn().getNodeName()

      

mistake:

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' ..

+3


source to share


1 answer


There is an environment variable NODE_NAME that has this.

It can be accessed as follows:



echo "NODE_NAME = ${env.NODE_NAME}"

      

When you edit a pipeline job, you can find all of the available environment variables by going to the "pipeline syntax" help link (to the left of the page), then find the "Global Variables" section and go to the "Global Variables" Variable Reference section. "env", which lists the available environment variables.

+6


source







All Articles