Answers: <p>I've done something similar to this in the past:</p>
  
  <pre><code>stage('Step Tests') {
    steps {
      dir('test') {
        script {
          try {
            timeout(time: 5, unit: 'MINUTES', activity: true) {
              sh "yarn step-tests"
            }
          } catch (Exception e) {
            currentBuild.result = 'FAILURE'
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I haven't tested this code exactly as I wrote it here, so think of this more as a kind of rough sketch rather than polished final product.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-05T16:14:01.497Z
Answers: Thanks, this worked. Interesting that `catchError` has different behavior. I also ended up using the `unstable` step instead of `currentBuild.result = 'UNSTABLE` due to https://issues.jenkins-ci.org/browse/JENKINS-45579
Answers: c32hedge
Answers: CC BY-SA 4.0
Answers: 2019-11-05T00:06:25.253Z
Answers: 2019-11-05T00:06:25.253Z
Answers: jayhendren
Body: <p>I have a stage in my Jenkins declarative pipeline that looks like:</p>
  
  <pre><code>stage('Step Tests') {
    steps {
      dir('test') {
        catchError(catchInterruptions: true, buildResult: 'FAILURE') {
          timeout(time: 5, unit: 'MINUTES', activity: true) {
            sh "yarn step-tests"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>The timeout activates if my tests don't output anything to the console for 5 minutes, and subsequent stages still execute, which is what I want. However, the build is still marked as aborted instead of failed, which I thought the <code>buildResult</code> parameter was for.</p>
  
  <p>How do I catch a timeout and mark the build as unstable or failed, instead of aborted?</p>
  
Tags: <jenkins><jenkins-pipeline><jenkinsfile>
Title: How to mark a step as failed or unstable on timeout, instead of aborting build?
UserDisplayName: c32hedge
ThreadText: # How to mark a step as failed or unstable on timeout, instead of aborting build?
  
  (Asked by: username_0 on 2019-11-04)
  
  I have a stage in my Jenkins declarative pipeline that looks like:
  
  ```
  stage('Step Tests') {
    steps {
      dir('test') {
        catchError(catchInterruptions: true, buildResult: 'FAILURE') {
          timeout(time: 5, unit: 'MINUTES', activity: true) {
            sh "yarn step-tests"
          }
        }
      }
    }
  }
  
  ```
  
  The timeout activates if my tests don't output anything to the console for 5 minutes, and subsequent stages still execute, which is what I want. However, the build is still marked as aborted instead of failed, which I thought the `buildResult` parameter was for.
  
  How do I catch a timeout and mark the build as unstable or failed, instead of aborted?
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-05. Score: 4 | Accepted answer
  
  I've done something similar to this in the past:
  
  ```
  stage('Step Tests') {
    steps {
      dir('test') {
        script {
          try {
            timeout(time: 5, unit: 'MINUTES', activity: true) {
              sh "yarn step-tests"
            }
          } catch (Exception e) {
            currentBuild.result = 'FAILURE'
          }
        }
      }
    }
  }
  
  ```
  
  I haven't tested this code exactly as I wrote it here, so think of this more as a kind of rough sketch rather than polished final product.
  
  > **Comments:**
  > **username_0**: Thanks, this worked. Interesting that `catchError` has different behavior. I also ended up using the `unstable` step instead of `currentBuild.result = 'UNSTABLE` due to https://issues.jenkins-ci.org/browse/JENKINS-45579  
  
  ---
  
  