Skip to content

Instantly share code, notes, and snippets.

@snallami
Created June 3, 2016 20:35
Show Gist options
  • Save snallami/dce71a66af5db39607eae7f8a69c2c91 to your computer and use it in GitHub Desktop.
Save snallami/dce71a66af5db39607eae7f8a69c2c91 to your computer and use it in GitHub Desktop.
Groovy script to update jenkins label
import hudson.model.*
import hudson.model.labels.LabelAtom
updateJenkinsLabel(Hudson.instance.items,"<Replace With Old Label>", "<Replace With new Label")
def updateJenkinsLabel(items, oldLabel, newLabel) {
for (item in items) {
// Find out if it is a folder
if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {
if(item instanceof FreeStyleProject || item instanceof hudson.maven.MavenModuleSet) {
if(item.assignedLabel != null && item.assignedLabel.name.equals(oldLabel) ) {
item.assignedLabel = new LabelAtom(newLabel)
item.save()
println("JOB: " + item.name + " has label "+oldLabel + " updated with "+newLabel)
}
}
} else {
updateJenkinsLabel(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems(), oldLabel, newLabel)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment