I have a few hundred jenkins nodes and need to update the labels in all of them.
It seems the easiest would be to run a groovy script in the Jenkins master script console that would add the extra label to each node's config.
Question: How would one do this in groovy?
In the script console available at <JENKINS-URL>/script
, execute the script after updating the "NEW_LABEL" string with the new label you want.
for (node in jenkins.model.Jenkins.instance.nodes) {
node.setLabelString(node.getLabelString() + " " + "NEW_LABEL")
}
The above will add a "NEW_LABEL" to every node. If you want to add "NEW_LABEL" to only those nodes that contains a given label then add a if
condition.
for (node in jenkins.model.Jenkins.instance.nodes) {
if (node.getLabelString().contains("EXISTING_LABEL")) {
node.setLabelString(node.getLabelString() + " " + "NEW_LABEL")
}
}
If you want to replace "EXISTING_LABEL" with "NEW_LABEL" then replace the label.
for (node in jenkins.model.Jenkins.instance.nodes) {
if (node.getLabelString().contains("EXISTING_LABEL")) {
node.setLabelString(node.getLabelString().replace("EXISTING_LABEL", "NEW_LABEL"))
}
}