Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile fail a step and continue the rest

I have a Jenkinsfile like this:

pipeline {
    agent any

    options {
        buildDiscarder(logRotator(numToKeepStr: '7'))
        disableConcurrentBuilds()
        timeout(time: 10, unit: 'MINUTES')
        timestamps()
    }

    stages {
        stage('Admin') {
            steps {
                script {
                    try {
                        result = "FAIL"
                    } catch(error) {
                        result = "FAIL"
                    }
                }
            }
        }
        stage('Normal') {
            steps {
                script {
                    try {
                sh("echo 'hi'")
                    } catch(error) {

                    }
                }
            }
        }
    }
}

How do I fail the first step and have it show red and have the pipeline continue for more steps?

I have looked at all the SO answers for this and can't make it work.

Setting result = "FAIL" does not cause the step to fail. How can I do this but continue the next step(s)?

like image 247
mikeb Avatar asked Sep 17 '25 05:09

mikeb


1 Answers

You can mark a build status as failed by setting a new value for the global jenkins variable currentBuild. The variable has 3 states: SUCCESS (green), FAILURE (red) and UNSTABLE (yellow). The catch should prevent the build from exiting so you can set the status of the currentBuild and simply continue.

           try {
               //so something
            } catch(error) {
                //mark build as failed
                currentBuild.result = 'FAILURE'
            }
        }

I think this should work. Sadly I don't have a way to validate this atm. If it does not work, please tell me.

like image 81
Michael Kemmerzell Avatar answered Sep 19 '25 16:09

Michael Kemmerzell