-
-
Save O5ten/016f03563c3eee09afcd63a8169775c4 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 " | |
} | |
"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Keeping this in my own forks as it was very useful.