At some point, if you are working on managing Jenkins for your team, you will find yourself looking for a set of useful groovy scripts to control jenkins configuration for a large no of builds/jobs.
Following are some mostly frequently needed ones
Get all jobs that belong to a view
Clear the build queue
Disable jobs in a view
Disconnect and re-connect to slaves
Following are some mostly frequently needed ones
Get all jobs that belong to a view
hudson.model.Hudson.instance.getView('my-view')?.items.each() {
println it.fullDisplayName
}
Clear the build queue
import hudson.model.*
def queue = Hudson.instance.queue
println "Queue contains ${queue.items.length} items"
queue.clear()
println "Queue cleared"
Disable jobs in a view
hudson.model.Hudson.instance.getView('1.1.x-tests')?.items.each() {
println("JOB : "+it.name)
it.disabled=true
it.save()
}
Get all jobs that that define a TimerTrigger with the trigger spec
import hudson.model.*
import hudson.triggers.*
for(item in Hudson.instance.items) {
for(trigger in item.triggers.values()) {
if(trigger instanceof TimerTrigger) {
println("--- Timer trigger for " + item.name + " ---")
println(trigger.spec + '\n')
}
}
}
Disconnect and re-connect to slaves
import hudson.model.*
Hudson.instance.computers.each { c ->
c.disconnect()
c.connect(true)
}