Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcusphi/d5a5d5769b69627a5169dc440d52a223 to your computer and use it in GitHub Desktop.
Save marcusphi/d5a5d5769b69627a5169dc440d52a223 to your computer and use it in GitHub Desktop.
/**
* Clean all non-locked lockable resources from Jenkins config.
*
* Rationale: When using lock function in pipeline with a dynamic string that will have many values
* over time, the Jenkins global configure page is filled with lockable resources.
*
* @param mailTo String - Comma separated list of email addresses to mail upon error
*/
def cleanLockableResources(String mailTo) {
try {
def res = GlobalConfiguration.all().get(org.jenkins.plugins.lockableresources.LockableResourcesManager.class).resources
def unlocked = res.findAll { it -> !it.locked }
println "Will remove ${unlocked.size()} lockable resources"
res.retainAll {it -> it.locked}
} catch (Throwable e) {
echo '==== Error mgmt: Do notifications ================'
try {
mail to: mailTo,
subject: "Build ${env.BUILD_DISPLAY_NAME} of ${env.JOB_NAME} failed",
body: "Build ${env.BUILD_DISPLAY_NAME} of ${env.JOB_NAME} failed\nBuild: ${env.BUILD_URL}"
} catch (Throwable err) {
echo 'ERROR: mail failure: ' + err.getMessage()
}
throw e
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment