尽管 Jenkins 管道中的前一阶段失败,如何在顺序阶段中执行下一阶段

How to execute next stage in sequential stages inspite of previous stage failure in Jenkins pipeline

目前我在 Jenkins 中定义了两个阶段,因为它们都需要不同的代理。

这是我的概念验证代码

stages {
    stage("Run Tests"){
        agent docker
        stage("Do setup"){}
        stage("Testing") {}
        post{
            always{
                echo "always"
            }
            failure {
                echo "failure"
            }
        }
    }
    stage("Generate Reports"){
        agent node-label
        stage("Generate"){

        }
    }
}

我需要在不同的代理上“生成报告”,因为某些二进制文件在节点上而不是在 docker 容器内。 docker 内的测试 运行 在节点上共享卷,因此我获得了在节点上生成报告所需的所有工件

(我曾尝试在 post 阶段 运行“生成报告”,但似乎 运行 在 docker 容器中。 )

现在,如果“运行 测试”失败,由于前一阶段失败,将跳过“生成报告”。知道如何强制“生成报告”阶段成为 运行,即使在前一阶段失败时也是如此。

下面是管道。

pipeline {
agent none
stages {
    stage("Run Tests"){
    agent { label "agent1" }
        stages  {           
            stage("Do setup"){
                steps{
                    sh 'exit 0'
                    }
                }
            stage("Testing") {
                steps {
                    catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh "exit 1"
                        } //catchError
                    } //steps
                post{
                    always{
                        echo "always"
                        }
                    failure {
                        echo "failure"
                        }
                    } // post
                } // Testing
            } // stages
        } // Run Test

    stage("Generate Reports"){
        agent { label "agent2" }
            steps {
                sh 'exit 0'
            }
        } // Reports
    }    
}

流水线成功,但是阶段Testing显示失败,可以选择buildResult和[=18=的状态]stageResult 如果您希望它不稳定或失败:

如果您想始终 运行“生成报告”阶段,那么您可以将早期阶段标记为 unstable(如果有失败)。
通过这种方式,Jenkins 将执行所有阶段,并且不会在特定阶段因错误而停止。
示例:

stages {
    stage("Run Tests"){
        agent docker
        stage("Do setup"){}
        stage("Testing") {
          steps {
                script {
            // To show an example, Execute "set 1" which will return failure 
            def result = bat label: 'Check bat......', returnStatus: true, script: "set 1"
                   if (result !=0) {
                   // If the result status is not 0 then mark this stage as unstable to continue ahead 
                   // and all later stages will be executed 
                   unstable ('Testing failed')
                   }
                }
            }
         }
    }
}
    stage("Generate Reports"){
        agent node-label
        stage("Generate"){

        }
    }
}

选项2,如果你不想通过return状态处理,你可以使用try and catch block

stage("Testing") {
          steps {
                script {
                 try {
                        // To show an example, Execute "set 1" which will return failure 
                         bat "set 1"
                   }
                   catch (e){
                   unstable('Testing failed!')
                   }
                }
            }
         }

选项 3:无论阶段是否失败,您都可以将 return 状态更改为完整构建成功。

stage("Testing") {
          steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                script {
                         // To show an example, Execute "set 1" which will return failure 
                         bat "set 1"
                   }
                }
            }
         

注意选项 3 有一个缺点,如果有任何错误,它不会在同一阶段执行进一步的步骤。
示例:
在此示例中,将不会执行打印消息“测试阶段”,因为 bat set 1 命令

中存在问题
stage("Testing") {
          steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
                script {
                         // To show an example, Execute "set 1" which will return failure 
                         bat "set 1"
                         // Note : Below step will not be executed since there was failure in prev step
                         println "Testing stage"
                   }
                }
            }

选项 4:您已经尝试将生成报告阶段保留在构建的 Post 始终
部分,以便始终执行,而不管任何失败。