终止长时间运行的构建记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def timeThreshold = 200 // seconds
def jobs = Jenkins.instance.getAllItems(hudson.model.Job.class)

jobs.each { job ->
def builds = job.getBuilds()
if(builds) {
builds.each {
if(it.isBuilding()) {
def duration = System.currentTimeMillis() - it.getStartTimeInMillis()
if (duration > timeThreshold * 1000) {
println("abort job:${job.name} buildNum:${it.number} duration:${duration}ms")
it.getExecutor().interrupt(hudson.model.Result.ABORTED)
}
}
}
} else {
println("${job.name} don't have builder, skip...")
}
}