Skip to content

Instantly share code, notes, and snippets.

@mlbright
Forked from seanf/jenkins-ensure-timeout.groovy
Last active August 29, 2015 14:18
Show Gist options
  • Save mlbright/f4e17d8249a82de53371 to your computer and use it in GitHub Desktop.
Save mlbright/f4e17d8249a82de53371 to your computer and use it in GitHub Desktop.
// 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.
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 {
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 = 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()
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 "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment