I would like to get the list of jobs starting with a given name, followed by updating the label node on which the job can run. I did the following and was not successful. Any inputs in what I am missing here.
import hudson.model.*;
import hudson.util.*;
import hudson.model.labels.*;
import jenkins.model.*;
import hudson.FilePath.FileCallable;
import hudson.slaves.OfflineCause;
import hudson.node_monitors.*;
buildableItems = Jenkins.instance.getAllItems.each {job ->
job.name.startsWith("Automation -")
println job.fullName;
}
for(item in buildableItems) {
job.assignedlabel = new LabelAtom('new-label-name')
item.save()
}
The
each
statement that you are suing is just going over the elements and running the closure on them but it doesn't return anything. Instead you should filter your list withfindAll
, then for returned filtered list run the code the changes the labels:Or alternatively use
each
and run both the condition and the configuration in the same iteration: