即使并行阶段失败,也要继续顺序阶段

时间:2021-06-16 11:32:28

标签: python jenkins jenkins-pipeline pipeline jenkins-groovy

我有一个这样的管道

pipeline {
agent any
options {parallelsAlwaysFailFast()}
stages {
    stage('Non-Parallel Stage') {
        steps {
            echo 'This stage will be executed first.'
        }
    }
    stage('Parallel Stage') {
        parallel {
            stage('Branch A') {
                agent {
                    label "trfw"
                }
                steps {
                    sh 'exit -1'  // fails here
                    echo "On Branch A"
                }
            }
            stage('Branch B') {
                agent {
                    label "trfw"
                }
                steps {
                    echo "On Branch B"
                }
            }
            stage('Branch C') {
                agent {
                    label "trfw"
                }
                stages {
                    stage('Nested 1') {
                        steps {
                            echo "In stage Nested 1 within Branch C"
                        }
                    }
                    stage('Nested 2') {
                        steps {
                            echo "In stage Nested 2 within Branch C"
                        }
                    }
                }
            }
        }
    }
    stage('validator Stage') {
        steps {
            echo 'This validator stage should run even after falure of other stages.'
        }
    }
  }
}

在上面的管道中,当一个并行阶段失败时,所有其他阶段都会失败(并行以及顺序),因为我在选项中使用了“parallelsAlwaysFailFast()”。我只想在失败的情况下使并行阶段失败,而不是顺序(验证器)阶段失败。有什么办法可以做到这一点吗?

Result

1 个答案:

答案 0 :(得分:0)

实现的一种方法是使用构建后操作。

pipeline {
agent any
options {parallelsAlwaysFailFast()}
stages {
    stage('Non-Parallel Stage') {
        steps {
            echo 'This stage will be executed first.'
        }
    }
    stage('Parallel Stage') {
        parallel {
            stage('Branch A') {
                agent {
                    label "trfw"
                }
                steps {
                    sh 'exit -1'  // fails here
                    echo "On Branch A"
                }
            }
            stage('Branch B') {
                agent {
                    label "trfw"
                }
                steps {
                    echo "On Branch B"
                }
            }
            stage('Branch C') {
                agent {
                    label "trfw"
                }
                stages {
                    stage('Nested 1') {
                        steps {
                            echo "In stage Nested 1 within Branch C"
                        }
                    }
                    stage('Nested 2') {
                        steps {
                            echo "In stage Nested 2 within Branch C"
                        }
                    }
                }
            }
        }
    }
  }
  post {
            always{
                    echo 'This should run even after failure of other stages.'
                }
        }

} 

Jenkins 输出 Jenkins Output