Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkins Groovy script: sets a timeout strategy for any job which doesn't have one
// This script is for Jenkins' Groovy console, and sets a timeout
// strategy for any job which doesn't have one.
// Based on http://janmaterne.wordpress.com/2010/07/11/how-to-check-if-all-hudson-jobs-have-a-timeout/
// Updated and modified by Sean Flanigan.
import hudson.model.*
String describe(strat) {
if (strat instanceof hudson.plugins.build_timeout.impl.ElasticTimeOutStrategy) {
return "Elastic(${strat.timeoutPercentage}, ${strat.numberOfBuilds}, ${strat.timeoutMinutesElasticDefault})"
} else if (strat instanceof hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy) {
return strat.timeoutMinutes
// } else if (strat instanceof hudson.plugins.build_timeout.impl.NoActivityTimeOutStrategy) {
// return "No activity(${strat.timeoutSecondsString})"
} else {
return strat?.descriptor?.displayName ?: "null"
}
}
hudsonInstance = hudson.model.Hudson.instance
allItems = hudsonInstance.allItems
activeJobs = allItems.findAll{job -> job instanceof BuildableItemWithBuildWrappers}
defaultFailBuild = true
println "Current | Est. (mins) | Name"
activeJobs.each { job ->
// Get the Timeout-PlugIn
wrapper = job.getBuildWrappersList().findAll{ it instanceof hudson.plugins.build_timeout.BuildTimeoutWrapper }[0]
//String currentTimeoutStr = currentTimeout
String currentTimeoutStr = describe(wrapper?.strategy)
// Get the current Timeout, if any
currentTimeout = (wrapper != null) ? wrapper.timeoutMinutes : ""
// Calculate a new timeout with a min-value
est = Math.round(job.estimatedDuration / 1000 / 60)
// Update the timeout, maybe requires instantiation
if (wrapper == null) {
def strat = new hudson.plugins.build_timeout.impl.LikelyStuckTimeOutStrategy()
// alternative: 8 hours absolute timeout
// def strat = new hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy("480")
plugin = new hudson.plugins.build_timeout.BuildTimeoutWrapper(strat, defaultFailBuild, true)
job.getBuildWrappersList().add(plugin)
action = "established"
} else {
// wrapper.timeoutMinutes = defaultTimeout
// action = "updated"
action = "unchanged"
}
// String preparation for table output
String estStr = est
estStr = estStr.padLeft(12)
currentTimeoutStr = currentTimeoutStr.padRight(12)
String jobname = job.name.padRight(60)
// Table output
println "$currentTimeoutStr | $estStr | $jobname | $action "
}
""
@seanf

This comment has been minimized.

@taylorp36

This comment has been minimized.

Copy link

taylorp36 commented Jan 4, 2016

thank you!

@timguy

This comment has been minimized.

Copy link

timguy commented Aug 28, 2017

great script.
Did only run with additional
import hudson.model.*
Maybe helfpful for others:
//8 hours absolute timeout
def strat = new hudson.plugins.build_timeout.impl.AbsoluteTimeOutStrategy("480")

@seanf

This comment has been minimized.

Copy link
Owner Author

seanf commented Aug 29, 2017

Thanks @timguy. I've incorporated those suggestions (with the alternative strategy commented out).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.