[英]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 测试,这是我得到的行为:
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.