Jenkins 声明式管道:检查以前的构建状态

Jenkins Declarative Pipeline: Check previous build status

使用 Jenkins declarative pipeline 检查前一阶段是否失败的最佳方法是什么,如果失败则 运行回滚命令。

我刚刚尝试如下,但它抛出如下错误。

Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild. Administrators can decide whether to approve or reject this signature.

    stage('Deploy to production'){
        when{
            beforeAgent true
            expression{return env.GIT_BRANCH == "origin/master"}
        }
        steps{
            script{
                    echo "Deploying production environment"
                    sh "helm install ...."

                }else {
                    error "Buid was not confirmed"
                }
    stage('Roll Back'){
        when{
            expression {
                !hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult()) == true
            }
        }
        steps{
            script{
                sh "helm rollback <release> 0"
            }
        }
    }
}

您可以使用 !("SUCCESS".equals(currentBuild.previousBuild.result)).

似乎 rawBuild 仅限于受信任的库(全局定义的库),或者您需要为该方法添加一个例外。

但直接使用 .previousBuild 应该可以。

https://opensource.triology.de/jenkins/pipeline-syntax/globals