我想让我的Jenkins部署管道
- 尝试shell命令,
- 提供一个输入步骤,如果该命令失败,然后
- 重新尝试命令并在"ok"下继续管道。
这是我尝试这样做的(开始)。
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]'
}
}
}
}
当直接在stage
中运行input
时,此示例确实显示了输入。然而,当把它放在post { failure }
,我得到这个错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 27: Missing required parameter: "message" @ line 27, column 21.
input {
^
Jenkins声明式管道允许post
中的input
吗?
有没有更好的方法来实现我想要的结果?
根据文档:
后置条件块包含与步骤部分相同的步骤。
这意味着代码中的输入将被解释为步骤而不是指令。
使用脚本语法的解决方案(try/catch也可以):
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]'
}
}
}
}
使用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]'
}
}
}
请记住,使用post section的方法将忽略结果第二个aws appconfig get-configuration [etc etc]
和fail。有一种方法可以改变这种行为,但我不认为这个解决方案是干净的。