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

Article ID:115001421891
1 minute readKnowledge base

Issue

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

Environment

  • Jenkins

  • CloudBees Jenkins Enterprise - Managed controller (CJEMM)

  • CloudBees Jenkins Team (CJT)

  • CloudBees Jenkins Platform - Client controller (CJPCM)

  • Docker plugin

  • Yet Another Docker plugin

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
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.