Issue
I'd like my Jenkins deploy pipeline to
- attempt a shell command,
- provide an input step if that command fails, and then
- re-try the command and continue the pipeline on "ok".
Here's the (start) of my attempt to do so.
stage('Get config') {
steps {
sh 'aws appconfig get-configuration [etc etc]'
}
post {
failure {
input {
message "There is no config deployed for this environment. Set it up in AWS and then continue."
ok "Continue"
}
steps {
sh 'aws appconfig get-configuration [etc etc]'
}
}
}
}
When running the input
directly in a stage
, this example does show the input. However, when putting it in the post { failure }
, I get this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 27: Missing required parameter: "message" @ line 27, column 21.
input {
^
Do Jenkins declarative pipelines allow input
in post
?
Is there a better way to accomplish my desired outcome?
Solution
As per documentation:
Post-condition blocks contain steps the same as the steps section.
This means that input in your code is interpreted as step instead of directive.
Solution using script syntax (try/catch would also be fine there):
stage('Get config') {
steps {
script {
def isConfigOk = sh( script: 'aws appconfig get-configuration [etc etc]', returnStatus: true) == 0
if ( ! isConfigOk ) {
input (message: "There is no config deployed for this environment. Set it up in AWS and then continue.", ok: "Continue")
sh 'aws appconfig get-configuration [etc etc]'
}
}
}
}
Using post section:
stage('Get config') {
steps {
sh 'aws appconfig get-configuration [etc etc]'
}
post {
failure {
input (message: "There is no config deployed for this environment. Set it up in AWS and then continue.", ok: "Continue")
sh 'aws appconfig get-configuration [etc etc]'
}
}
}
Remember that your approach with post section will ignore outcome of second aws appconfig get-configuration [etc etc]
and fail. There is a way to change this behaviour but I wouldn't call this solution any clean.
Answered By - Dawid Fieluba
Answer Checked By - Timothy Miller (JavaFixing Admin)