Home > Mobile >  Complete the whole Jenkins pipeline after all the retry attempt is failed for a certain stage, witho
Complete the whole Jenkins pipeline after all the retry attempt is failed for a certain stage, witho

Time:11-24

I've a retry block inside of a stage with attempt count of 5. When all my retry attempts are completed for that stage and it still turns out to be failed, the further stages get skipped and the pipeline gets aborted. What I want is, even if the stage with retry block gets failed, the next stages must not be skipped and should run as usual. What I have tried till now is,

stage('Run Test') {
try {
  echo "Hello World"
}
catch(error) {
  retry(5) {
     try {
       input "Retry?"                       
       echo "Hello World in catch"
     }
     catch(err) {
        //I want here to continue for the next stage rather than skipping the remaining stages and 
        //abort the pipeline
     }
  }
}
}

Any help will be much appreciated.

CodePudding user response:

You can use the retry step alongside the catchError step to achieve the desired functionality.

catchError: Catch error and set desired build result.
If the body throws an exception, mark the build as a failure, but nonetheless continue to execute the Pipeline from the statement following the catchError step.
The behavior of the step when an exception is thrown can be configured to print a message, set a build result other than failure, change the stage result, or ignore certain kinds of exceptions that are used to interrupt the build. This step is most useful when used in Declarative Pipeline or with the options to set the stage result or ignore build interruptions.

So you can wrap you retry code block with the catchError and set the build and stage result to the value you want in case the retry block fails, regardless of the result the execution will continue on to the following steps.
Something like:

stage('Run Test') {
    echo "Hello World"
    catchError(buildResult: 'SUCCESS', stageResult: 'SUCCESS') {
        retry(5) {
            // retry code block
        }
    }
    // following steps will always be executed
}

The most used options for the result are: FAILURE, SUCCESS and UNSTABLE.
See more options and explanation in the following answer

  • Related