简体   繁体   中英

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:

  • 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

error Command failed with exit code 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM