Skip to content

Instantly share code, notes, and snippets.

@lwille
Last active February 6, 2020 14:17
Show Gist options
  • Save lwille/ee63924f8fb913b4547c8247326c92b1 to your computer and use it in GitHub Desktop.
Save lwille/ee63924f8fb913b4547c8247326c92b1 to your computer and use it in GitHub Desktop.
Jenkins script console: enable specific builds
import com.cloudbees.hudson.plugins.folder.Folder
import groovy.transform.Field
import hudson.model.*
import org.jenkinsci.plugins.workflow.job.WorkflowJob
@Field def fmt = "%-40s | %-10s | %-20s | %-20s | %-10s\n"
printf(fmt, "job", "branch", "queue time/status", "label", "last build")
printf(fmt, '-' * 40, '-' * 10, '-' * 20, '-' * 20, '-' * 10)
enableBuilds(Hudson.instance.items, 'INFRA-925', false)
def enableBuilds(items, setBranch = null, trigger = true, abortRunning = false) {
for (item in items) {
if (item instanceof Folder) {
enableBuilds((item as Folder).getItems(), trigger, abortRunning)
} else if (item.class != WorkflowJob) {
if (!item.name.contains("seed-job")) continue
item.disabled = false
if (setBranch) {
item.getSCMs().first().branches = [setBranch]
}
item.save()
if (abortRunning) {
abortAll(item.builds)
}
if (trigger) {
hudson.model.Hudson.instance.queue.schedule(item, 0)
}
def branch = item.getSCMs().first().branches?.first()
def status = (item.building ? 'building' : (Hudson.instance.queue.items.find { it.task.is item }?.getInQueueForString() ?: 'idle'))
def lastStatus = item.lastBuild?.getBuildStatusSummary()?.message ?: "n/a"
if (lastStatus == "stable") lastStatus = "✓ ${item.lastBuild.getDurationString()}"
printf fmt, item.name, branch, status, item.getAssignedLabel(), lastStatus
}
}
}
def abortAll(builds) {
builds.each {
FreeStyleBuild run = (FreeStyleBuild) it
if (!it.building || !run.executor) {
return
}
run.executor.interrupt(Result.ABORTED)
println Result.ABORTED
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment