Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Declarative Pipeline: Check previous build status

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"
            }
        }
    }
}
like image 534
semural Avatar asked Nov 23 '25 14:11

semural


1 Answers

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

like image 107
Adam Avatar answered Nov 25 '25 07:11

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!