How to continue past a failing stage in Jenkins declarative pipeline syntax

后端 未结 4 518
深忆病人
深忆病人 2021-02-01 06:14

I want to define multiple stages in Jenkins declarative pipeline syntax which can continue past any one of them failing. I cannot find any existing questions which are true dupl

4条回答
  •  忘了有多久
    2021-02-01 07:04

    I think it depends on how dependent the jobs are on each other. Derived from your example I would assume that

    • stage 1 is independent from all other stages, because it is the first
    • stage 2 is independent from all other stages, because stage 1 might fail immediatly and stage 2 would still be required to run
    • stage 3 depends on the result of stage 1 and stage 2

    So the correpsonding pipeline could be

    pipeline {
        stages {
            stage('Independent tasks') {
                parallel {
                    stage('stage 1') {
                        steps {
                            sh 'exit 1' // failure
                        }
                    }
                    stage('stage 2') {
                        steps {
                            echo 'Happens even so stage 1 fails'
                            sh 'exit 0' // success
                        }
                    }
                }
                post {  // 'stage 3'
                    failure {
                        echo "... at least one failed"
                    }
                    success {
                        echo "Success!"
                    }
                }
            }
            stage ('stage 4') {
                steps {
                    echo 'Happens only if all previous succeed'
                }
            }
        }
    }
    

    stage 1 and stage 2 will always run, stage 3 reacts on their combined success/failure.


    Additional thought: This concept only works 'at the end' of your pipeline. In case you need it somewhere in the middle AND the build has to continue, you could move it into an own job and use the build job plugin.

    pipeline {
        stages {
        stage('Start own job for stage 1, 2, 3') {
            steps {
                build job: 'stageOneTwoThree', propagate: false, wait: true
            }
        }
        stage ('stage 4') {
            steps {
                echo 'Happens always, because "propagate: false"'
            }
        }
    }
    

提交回复
热议问题