Jenkinsfile:在后置条件块中要求输入

问题描述

我希望我的 Jenkins 部署管道

  1. 尝试一个 shell 命令,
  2. 如果该命令失败,请提供输入步骤,然后
  3. 重试该命令并在“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]'
                }
            }
        }
    }

直接在 input 中运行 stage 时,此示例确实显示了输入。但是,将其放入 post { failure } 时,出现此错误

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup Failed:

WorkflowScript: 27: Missing required parameter: "message" @ line 27,column 21.

                       input {

                       ^

Jenkins 声明式管道是否允许在 input 中使用 post

有没有更好的方法来实现我想要的结果?

解决方法

根据documentation

Post-condition 块包含与 steps 部分相同的 steps

这意味着您代码中的 input 被解释为 step 而不是指令。


使用脚本语法的解决方案(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]'
            }
        }
    }
}

使用帖子部分:

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 部分的方法将忽略第二个 aws appconfig get-configuration [etc etc] 的结果并失败。有一种方法可以改变这种行为,但我不会称这种解决方案很干净。