Get the Cloud name of an agent provisoned by the (Yet Another) Docker plugin

Issue

  • Find the name of the Cloud that a docker agent belong to

Environment

Resolution

The agents provisioned by both the Yet Another Docker plugin and the Docker plugin contains the name of the Cloud that is configured globally:

They automatically join the cloud name and the container id as following: <dockerCloudName>-<agentId>. You can therefore use a regexp on the variable ${NODE_NAME} to extract the <dockerCloudName> part:

def getDockerCloudName(dockerNodeName) {
    return ("${dockerNodeName}" =~ /(.*)-([a-z0-9]+)/)[0][2];
}

node ('myDockerLabel') {
    echo "${getDockerCloudName(env.NODE_NAME)}"
}

Example of the output:

Started by user admin
[Pipeline] node
Running on myDockerLabel-a55e069acece in /home/jenkins/workspace/testDir
[Pipeline] {
[Pipeline] echo
a55e069acece
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.