【问题标题】:Jenkins: How to fail stage when test fails?詹金斯:测试失败时如何进入失败阶段?
【发布时间】:2023-01-18 21:50:39
【问题描述】:

我的管道中有一个阶段正在运行一些 UI 测试,这是我得到的行为:

  • 如果测试通过,该阶段变为绿色,下一阶段将运行,并且在 最后构建变绿。
  • 如果测试失败,阶段变为绿色,下一阶段运行,并在 结束构建是黄色的(不稳定)

我怎样才能做到这一点,而不是在测试失败时进入下一阶段,而是管道失败?

这是我的管道阶段,我尝试添加一个帖子部分,但即使测试失败,它也会报告成功。

                stage('UITests') {
                    steps {
                        withCredentials([file(credentialsId: 'env_file', variable: 'envFile')]) {
                            sh '''
                            cat $envFile > .env.dev
                            make run_tests
                            '''
                            }  
                    }
// Fail build if test fail 
                    post{
                            success {
                                echo "UI Tests passed moving to Build stage"
                            }
                            failure {
                                error "UI Tests Failed, stopping the build"
                            }}
                }

在该阶段的 Jenkins 日志中,我可以看到测试失败的时间

错误 命令失败,退出代码为 1。

测试通过时不会发生这种情况,那么是否有理由让 post 块总是成功?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    sh 步骤返回与实际 sh 命令相同的状态代码:https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

    您可以获得此状态代码,将其值分配给变量,然后失败并显示一条自定义消息:

    def statusCode = sh script:"cat $envFile > .env.dev && make run_tests", returnStatus:true
    if (statusCode != 0) {
      error 'UI Tests Failed, stopping the build'
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      相关资源
      最近更新 更多