How to mark a step as failed or unstable on timeout, instead of aborting build?

7,346

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.

Share:
7,346

Related videos on Youtube

Şahin Taşın
Author by

Şahin Taşın

Developer turned software engineer in test, with a strong interest in helping my team deliver reliable, testable, maintainable software.

Updated on September 18, 2022

Comments

  • Şahin Taşın
    Şahin Taşın over 1 year

    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?

  • Şahin Taşın
    Şahin Taşın over 4 years
    Thanks, this worked. Interesting that catchError has different behavior. I also ended up using the unstable step instead of currentBuild.result = 'UNSTABLE due to issues.jenkins-ci.org/browse/JENKINS-45579