|
// |
|
// Coloured Messages: http://testerfenster.com/blog/jenkins-tutorials-add-color-to-console-output/ |
|
// |
|
String boldGreenMessage(final String message) { return "\033[1;32m${message}\033[0m" } |
|
String boldBlueMessage(final String message) { return "\033[1;34m${message}\033[0m" } |
|
String boldRedMessage(final String message) { return "\033[1;31m${message}\033[0m" } |
|
String boldYellowMessage(final String message) { return "\033[1;33m${message}\033[0m" } |
|
String triplePrefixMessage(final Closure<String> colour, final String prefix, final String message) { |
|
def colouredPrefix = "${colour("${prefix}")}" |
|
return "${colouredPrefix}\n${colouredPrefix} ${message}\n${colouredPrefix}" |
|
} |
|
void successMessage(final String message) { ansiColor('xterm') { echo triplePrefixMessage(this.&boldGreenMessage, '[SUCCESS]', message) } } |
|
void infoMessage(final String message) { ansiColor('xterm') { echo triplePrefixMessage(this.&boldBlueMessage, '[INFO]', message) } } |
|
void warningMessage(final String message) { ansiColor('xterm') { echo triplePrefixMessage(this.&boldYellowMessage, '[WARNING]', message) } } |
|
void errorMessage(final String message) { ansiColor('xterm') { echo triplePrefixMessage(this.&boldRedMessage, '[ERROR]', message) } } |
|
|
|
// |
|
// Retry, Continue or Abort on errors |
|
// |
|
public <R> R retryContinueOrAbort(final Closure<R> action, final int count = 0) { |
|
|
|
infoMessage "Trying action, attempt count is: ${count}" |
|
|
|
try { |
|
return action.call(); |
|
} catch (final exception) { |
|
|
|
errorMessage exception.toString() |
|
|
|
def userChoice |
|
timeout(time: 30, unit: 'MINUTES') { |
|
userChoice = input( |
|
message: 'Something went wrong, what do you want to do next?', |
|
parameters: [ |
|
choice( |
|
name: 'Next Action', |
|
choices: ['Retry', 'Continue', 'Abort'].join('\n'), |
|
description: 'Whats your next action?' |
|
) |
|
] |
|
) |
|
} |
|
|
|
switch (userChoice) { |
|
case 'Retry': |
|
warningMessage 'User has opted to try the action again.' |
|
return retryContinueOrAbort(action, count + 1) |
|
case 'Continue': |
|
warningMessage 'User has opted to continue past the action, they must have manually fixed things.' |
|
return null; |
|
default: |
|
errorMessage 'User has opted to abort the action' |
|
throw exception; |
|
} |
|
} |
|
}; |
|
|
|
// |
|
// Test Pipeline Script |
|
// |
|
pipeline { |
|
agent none |
|
stages { |
|
stage('Reach the bridge') { |
|
steps { |
|
infoMessage 'Stop. Who would cross the Bridge of Death must answer me these questions three, ere the other side he see.' |
|
} |
|
} |
|
stage('Answer a question') { |
|
steps { |
|
script { |
|
favouriteColour = retryContinueOrAbort { |
|
infoMessage 'What... is your favourite colour?' |
|
error 'Blue. No, yel... [he is also thrown over the edge] auuuuuuuugh.' |
|
} |
|
} |
|
} |
|
} |
|
stage('Get POM Version') { |
|
steps { |
|
successMessage 'You have made it past the bridge keeper!' |
|
script { |
|
mavenVersion = retryContinueOrAbort { |
|
"0.0.1-SNAPSHOT" |
|
} |
|
} |
|
} |
|
} |
|
stage('Print Variables') { |
|
steps { |
|
successMessage "favouriteColour: ${favouriteColour}" |
|
successMessage "mavenVersion: ${mavenVersion}" |
|
} |
|
} |
|
} |
|
} |
@gianpaolof depending on what your trying to achieve, that's not that the
retryCotninueorAbort
script it trying to achieve. As it will try running the passed code block, if it fails it will prompt you as to whether you want to retry it, continue as if nothing had failed or give up.The main purpose was to provide a means of manually fixing things that went wrong during a deployment, that couldn't be automatically retired and once fixed you could just continue pass.
If you are after automatic retry on failure, up to a certain amount I believe the built in
retry
function might do what you want. https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-timesThat said if you want it to always run the command twice, native groovy loops should just work these days with the latest versions.
Now I've written this out, there is also probably a bug in your code snippet anyway as the
sh
command will execute and assign the output tomyscript
before being passed intoretryContinueOrAbort
and attempted to be treated as a function/method/closure.So it could look like this and work, in theory.
Disclaimer this is all written off the top of my head plus a little bit of a Google to refresh on syntax.