What is the best way to check whether if previous stage failed or not using Jenkins declarative pipeline and if it is failed then run rollback command.
I just tried as below but it throws an error as below.
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"
}
}
}
}
You can use !("SUCCESS".equals(currentBuild.previousBuild.result))
.
It seems that rawBuild
is restricted to trusted libraries (Globally defined libraries), or you'll need to add an exception to that method.
But using .previousBuild
directly should work.
https://opensource.triology.de/jenkins/pipeline-syntax/globals