Skip to content

Instantly share code, notes, and snippets.

@seanf
Last active September 27, 2022 11:00
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save seanf/8c0933694a9383152bc2 to your computer and use it in GitHub Desktop.
Save seanf/8c0933694a9383152bc2 to your computer and use it in GitHub Desktop.
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 "
}
""
@t-patt
Copy link

t-patt commented Jan 4, 2016

thank you!

@timguy
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
Copy link
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