Skip to content

Instantly share code, notes, and snippets.

@marcusphi
Last active November 6, 2019 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusphi/3380f83964249b1250a6d5230be741e5 to your computer and use it in GitHub Desktop.
Save marcusphi/3380f83964249b1250a6d5230be741e5 to your computer and use it in GitHub Desktop.
def myFolder = "JobDSL"
def myJob = 'maintenance-LockableResourceGC'
def mailTo = 'me@company.com' // Whitespace-separated list of recipient addresses
pipelineJob("${myFolder}/${myJob}") {
description("""Cleanup lockable resources on a scheduled basis\n""" + generatedByString())
logRotator { numToKeep(10) }
concurrentBuild(false)
// TRIGGERS ------------------------
triggers {
cron('0 3 * * *')
}
// WRAPPERS ------------------------
wrappers {
timestamps()
}
// DEFINITION ----------------------
definition {
cps {
script('''
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
}
''')
}
}
}
queue("${myFolder}/${myJob}")
def generatedByString() {
"<p>Generated by <a\n\
href='${JOB_URL}'\n\
>${JOB_NAME}</a>.\n\
<b>Do not modify manually!</b></p>"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment