使用Jenkins 声明性 管道检查上一阶段是否失败的最佳方法是什么,如果失败则运行回滚命令。
我只是如下尝试,但它抛出如下错误。
不允许使用方法的脚本 org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild。管理员可以决定是批准还是拒绝 此签名。
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"
}
}
}
}
答案 0 :(得分:3)
您可以使用!("SUCCESS".equals(currentBuild.previousBuild.result))
。
似乎rawBuild
仅限于受信任的库(全局定义的库),否则您需要为该方法添加一个例外。
但是直接使用.previousBuild
应该可以。
https://opensource.triology.de/jenkins/pipeline-syntax/globals