<p>I answered that question <a href="https://stackoverflow.com/questions/44494238/mark-jenkins-build-as-success-in-case-of-timeout-on-input-declarative-pipelin/60383321#60383321">stackoveflow</a> and I'll post the answer here:</p>

<p>For a Declarative pipeline (not a scripted) you would do something like this</p>

<pre><code>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
</code></pre>

<p>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).</p>

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

CC BY-SA 4.0
2020-04-16T13:05:02.483Z
2020-04-16T13:05:02.483Z
Guillermo Mansilla
<p>I've a pipeline with stages where one of the stage, intermittently takes longer than expectation and hence using <code>timeout</code> to abort it. But if the stage is aborted, build also marked as aborted. Following is the code for pipeline -</p>

<pre><code>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"
                }
            }
        }
    }
}
</code></pre>

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

CC BY-SA 4.0
2020-04-15T16:07:15.897Z
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
CC BY-SA 4.0
2020-04-16T17:35:18.610Z
https://stackoverflow.com/q/44494238/1824868
jayhendren
<jenkins><jenkins-pipeline><jenkinsfile>
How to mark build success when one of the stages is aborted?
Alpha
# How to mark build success when one of the stages is aborted?

(Asked by: username_0 on 2020-04-15)

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?

**Tags:** jenkins, jenkins-pipeline, jenkinsfile

> **Comments:**
> **username_1**: 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.  
> **username_1**: https://stackoverflow.com/q/44494238/1824868  

---

## 1 answers

### Answer by username_2 on 2020-04-16. Score: 2

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.


---

