Created
June 13, 2016 14:53
-
-
Save ulich/b80f93681ae1e1805f4a06113cbb4ecb to your computer and use it in GitHub Desktop.
Jenkins pipeline utility method that asks the user to retry the given closure and executes a closure on success or user abortion
This file contains hidden or 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
/** | |
* Executes given params.task closure and if it fails, asks the user if it should be retried. | |
* The task will then be executed again. If the user clicks abort, an exception will be | |
* thrown aborting the pipeline. | |
* | |
* After the task either completes successfully or after the user clicks abort, | |
* the params.andFinally closure will be executed. | |
* | |
* Usage: | |
* <pre><code> | |
* retry task: { | |
* sh 'something-flaky-generating-html-reports' | |
* }, andFinally: { | |
* publishHTML target: [reportDir: 'report', reportFiles: 'index.html', reportName: 'Report'] | |
* } | |
* </pre></code> | |
* | |
* @params params [task: {}, andFinally: {}] | |
*/ | |
def retry (params) { | |
// waitUntil will retry until the given closure returns true | |
waitUntil { | |
try { | |
// execute the task, if this fails, an exception will be thrown | |
// and the params.andFinally() wont be called | |
(params.task)() | |
(params.andFinally)() | |
// make waitUntil stop retrying this closure | |
return true | |
} | |
catch(e) { | |
try { | |
// input asks the user for "Retry? Proceed or Abort". If the | |
// user clicks Proceed, input will just return normally. | |
// If the user clicks Abort, an exception will be thrown | |
input "Retry?" | |
} | |
catch (userClickedAbort) { | |
// user clicked abort, call the andFinally closure and re-throw the exception | |
// to actually abort the pipeline | |
(params.andFinally)() | |
throw userClickedAbort | |
} | |
// make waitUntil execute this closure again | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment