简体   繁体   English

Jenkins:测试失败时如何进入失败阶段?

[英]Jenkins: How to fail stage when test fails?

I have a stage in my pipeline that is running some UI tests currently this is the behaviour I get:我的管道中有一个阶段正在运行一些 UI 测试,这是我得到的行为:

  • If the tests pass the stage goes green the next stage runs, and at the end the build goes green.如果测试通过,则该阶段变为绿色,下一阶段将运行,最后构建变为绿色。
  • If a test fails the stage goes green, the next stage runs, and at the end the build is yellow (unstable)如果测试失败,阶段变为绿色,下一阶段运行,最后构建为黄色(不稳定)

How can I make it so that instead of moving on to the next stage if a test fails the pipeline is failed?我怎样才能做到这一点,而不是在测试失败时进入下一阶段,而是管道失败?

This is the stage of my pipeline, I've tried adding a post section but even when a test fails it reports success.这是我的管道阶段,我尝试添加一个帖子部分,但即使测试失败,它也会报告成功。

                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"
                            }}
                }

In the Jenkins log for the stage I can see when a test fails I get在该阶段的 Jenkins 日志中,我可以看到测试失败的时间

error Command failed with exit code 1.错误 命令失败,退出代码为 1。

This doesn't happen when a test passes so is there a reason the post block is always going to success?测试通过时不会发生这种情况,那么帖子块总是成功的原因是什么?

The sh step returns the same status code that your actual sh command: https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script sh 步骤返回与实际 sh 命令相同的状态代码: https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

You can get this status code, assign its value to a variable and then fail with a custom message:您可以获得此状态代码,将其值分配给变量,然后失败并显示一条自定义消息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当覆盖率太低时,如何使 Jenkins 阶段失败并变成红色(使用 C# 和 dotnet 测试)? - How to make Jenkins stage fail and turn red when coverage is too low (using C# and dotnet test)? 如何仅在 jenkins 管道中的阶段 A 失败时触发阶段 B - How to trigger stage B only when stage A fails in jenkins pipeline 当ng测试失败时,如何在ng build --prod和jenkins作业失败之前运行ng test? - How to run ng test before ng build --prod and fail jenkins job when ng test fails? 测试失败时 Jenkins 中的 Docker 多阶段构建访问测试报告 - Docker Multi Stage Build access Test Reports in Jenkins when Tests Fail 詹金斯允许舞台失败 - Jenkins allow stage to fail 当 Mercurial 更新失败时,如何使 Jenkins 构建失败 - How to fail Jenkins build when Mercurial Update fails 如果我的构建阶段在jenkins管道中失败,如何跳过阶段? - how to skip the stage if my build stage fails in jenkins pipeline? CasperJs + jenkins:当测试失败时,如何检索此测试的所有信息 - CasperJs + jenkins : when a test fails, how to retrieve all information on this test 如何解决 Jenkins 上的 python“测试”阶段错误? - How to solve a python "test" stage error on Jenkins? 测试失败时如何使Newman的节点脚本失败 - How to make a node script of Newman fail when a test fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM