Jeff
Jeff

Reputation: 1879

Jenkins: Catch error to skip to next stage, but still show errored stage has failed

I am currently using Jenkins catchError to catch if an error has occurred within a stage, and if so, then skip to the next stage.

What I would like is to present that if a stage had caught an error, to presented in Jenkins as Red (Failed), even though this stage was skipped to the next.

This is how Jenkins pipeline stage is coded to catch error is script fails:

    stage('test_run') {
        steps {
            catchError {
                sh """#!/bin/bash -l
                    ***

                    npm run test:run:reporter
                """
            }
        }
    }

I found this solution in StackOverflow: Jenkins: Ignore failure in pipeline build step

This solution works, but in Jenkins' run, the stage that failed is presenting Green (aka Success).

This Jenkins run indicates that it failed:

Failed Jenkins Run

The following stage actually the cause of the failure and was skipped on caught error, however, this stage is showing Green (Success) and prefer to present it to be Red (Failed):

Failed stage skipped showing Green Success

The final post actions is showing as Yellow (Unstable), when normally it shows as Green (Success):

Unstable

Thank you for reading, always appreciate the assistance.

Upvotes: 5

Views: 13711

Answers (2)

Jeff
Jeff

Reputation: 1879

@DashrathMundkar Thank you

Using catchError() with the buildResult and stageResult, however I set the values to both 'FAILURE', and that did the trick.

catchError(buildResult: 'FAILURE', stageResult: 'FAILURE', message: 'Test Suite had a failure') { }

Upvotes: 0

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9174

You can use

catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
  Add your commands
}

Add whatever result you want to set for build and stage result.

Upvotes: 5

Related Questions