1

I've a pipeline with stages where one of the stage, intermittently takes longer than expectation and hence using timeout to abort it. But if the stage is aborted, build also marked as aborted. Following is the code for pipeline -

pipeline {
    agent any

    stages {

        stage('First') {
            options {
                timeout(time: 10, unit: 'SECONDS')
            }
            steps {
                script {

                    catchError(buildResult: 'SUCCESS') {
                        echo "Executing stage I"
                        sleep 12
                    }

                }
            }
        }

        stage('Second') {
            steps {
                script {
                    echo "Executing stage II"
                }
            }
        }
    }
}

Even though the stage is marked as Aborted, I want to mark build as Success. Can you please help how I can achieve this?

2
  • I would really have to play around with this to get it right. However, I can tell you that you want your timeout step inside the script block, and you also want to use try { } catch { } instead of catchError, and you want to make sure that timeout is inside your try block. Finally, you want to make certain you are only catching the exception raised on timeout and not all exceptions so that you don't accidentally mark an actual failed build as successful; you only want to catch the ones that hit the timeout. – jayhendren Apr 15 '20 at 16:07
1

I answered that question stackoveflow and I'll post the answer here:

For a Declarative pipeline (not a scripted) you would do something like this

stage('Foo') {
      steps {
            script {
                env.PROCEED_TO_DEPLOY = 1
                try {
                    timeout(time: 2, unit: 'MINUTES') {
                        // ...
                    }
                } catch (err) {
                    env.PROCEED_TO_DEPLOY = 0
                }
            }
      }
  }
  stage('Bar') {
    when {
        expression {
            env.PROCEED_TO_DEPLOY == 1
        }
    }
    //rest of your pipeline

stage "Bar" would be skipped but as for the rest of the stages the job will be marked as passed (assuming nothing wrong happened before).

As for your particular use-case, the trick is to use a try/catch block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.