1
votes

I'm setting up Jenkins pipeline which is mentioned below. My build gets aborted if the 1st stage is got failed but I want to execute 1st all stage and steps which are mentioned in stages.

pipeline {
agent none

stages {
    stage("build and test the project") {
        agent {
            docker "coolhub/vault:jenkins"
        }
        stages {
           stage("build") {
               steps {
                   sh 'echo "build.sh"'
               }
           }
           stage("test") {
               steps {
                   sh 'echo "test.sh" '
               }
           }
        }
    }
  }
}

I'd like to execute 1st all stage and steps which are mentioned in stages. after all, stage gets executed then finally need to get abort Jenkins job and show stage and steps which are failed.

2

2 Answers

1
votes

Yeah, well there's no way currently to do that apart from try catch blocks in a script. More here: Ignore failure in pipeline build step.

stage('someStage') {
    steps {
        script {
            try {
                build job: 'system-check-flow'
            } catch (err) {
                echo err
            }
        }
        echo currentBuild.result
    }
}
0
votes

In hakamairi's answer, the stage is not marked as failed. It is now possible to fail a stage, continue the execution of the pipeline and choose the result of the build:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh "exit 1"
                }
            }
        }
        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:

Pipeline Example

As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.

Just make sure your Jenkins is up to date, since this is a fairly new feature.