Continue Jenkins pipeline past failed stage

房东的猫 提交于 2019-11-30 02:54:36

This is now possible. Below is an example of a declarative pipeline, but catchError works for scripted pipelines as well.

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:

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.

EDIT: You need "Pipeline: Basic Steps" 2.16 (May 14, 2019)

I had the same concern, I was able to resolved it doing this.

Second stage will show in red and marked as failed meanwhile rest of the stages will keep running. You can set a flag and at the end of the stages checking that flag you can inform the status of the whole build.

node {

    def build_ok = true

    stage('one') {
        sh 'exit 0'
    }

    try{
        stage('two') {
            sh 'exit 1'   // failure
        }
    } catch(e) {
        build_ok = false
        echo e.toString()  
    }

    stage('three') {
        sh 'exit 0'
    }

    ....

    if(build_ok) {
        currentBuild.result = "SUCCESS"
    } else {
        currentBuild.result = "FAILURE"
    }
}

This should work. However all boxes are red if even only one fails, but you can see boxes with error marked, so you will easily distinguish failed jobs.

def indexes = ['one', 'two', 'three']

node() {
    for (index in indexes) {
        catchError {
            stage(index) {
                println index
                sh '''echo "123"'''
            }
        }
    }
}

Use

propagate: false

flag to move to next stage when previous stage fails.

example:

stage('<stage-name>'){
    node('<node-name>'){
        build job: '<job-name>', propagate: false
    }
}

stage('<stage-name>'){
    node('<node-name>'){
        build job: '<job-name>'
    }
}

It depends whether you are using declarative pipeline syntax or scripted pipeline syntax.

declarative pipeline syntax:

pipeline {
    agent any
    stages {
        stage('one') {
            steps {
                sh 'exit 0'
            }
        }
        stage('two') {
            steps {
                sh 'exit 1'   // failure
            }
        }
    }
    post {
        always {
            sh 'exit 0'
        }
    }
}

Post-condition blocks contain steps the same as the steps section.

scripted pipeline syntax:

node {

    def build_ok = true

    stage('one') {
        sh 'exit 0'
    }

    try{
        stage('two') {
            sh 'exit 1'   // failure
        }
    } catch(e) {
        build_ok = false
        echo e.toString()  
    }

    stage('three') {
        sh 'exit 0'
    }

    if(build_ok) {
        currentBuild.result = "SUCCESS"
    } else {
        currentBuild.result = "FAILURE"
    }
}

I resolved that, using post actions: https://jenkins.io/doc/pipeline/tour/post/

           post {
            always {
                ...
            }
           }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!