0

I have a pipeline with running unit tests, when one of them fail it mark build as Unstable, but I need to fail this build and make it red.

Here is an example of my pipeline

def gitStatus(buildStatus) {
 if (buildStatus == 'SUCCESS') {
updateGitlabCommitStatus(name: 'build', state: 'success')
addGitLabMRComment comment: 'Build Passed'
}
updateGitlabCommitStatus(name: 'build', state: 'failed')
addGitLabMRComment comment: "Something unexpected happened. Inspect Jenkins logs."
emailext subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', to: ''
}

try {
node ('Builder_1') {
    stage('Preparing source tree') {
                    }

stage('Install Dependencies') {
        sh '''#!/bin/bash
           npm install --prefer-offline
      '''
        }

try {

 stage ('Run Tests') {
withEnv(["JEST_JUNIT_OUTPUT=./jest-test-results.xml"]) {
 sh 'npm run test -- --ci --testResultsProcessor="jest-junit"'
}
 junit 'jest-test-results.xml'
}   

      } catch(err) {
        step([$class: 'JUnitResultArchiver', testResults: './jest-test-results.xml'])
       throw err
      }

 stage('Build') {
        sh '''#!/bin/bash
           npm run build
      '''
        }
}
}catch (e) {
if (currentBuild.result != 'SUCCESS'){
 currentBuild.result == 'FAILURE'
 }
 throw err
 } finally {
    gitStatus(currentBuild.currentResult)
 }

Could you please suggest your thoughts, what is going wrong.

1 Answers1

0

Instead of throw err, if you use error err or sh "exit 1". It will mark the build as red. Hope this helps.

Follow this post for more info

SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33