Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-56683

Declarative Pipeline: set the build result when tests fail without a script block

      pipeline {
      
          agent any
      
          stages {
              stage ('Build & Test') {
                  steps {
                      sh 'echo Building... Failure here will fail the build'
                      script {
                          try {
                              echo 'Running tests...'
                              sh 'exit 1'
                          }
                          catch (exc) {
                              echo 'Testing failed!'
                              currentBuild.result = 'UNSTABLE'
                          }
                      }
                  }
              }
          }
      }
      

      E.g. as described here https://lastviking.eu/how_to_set_result_as_unstable_from_declarative_pipeline.html

      It would be nice to have a way to set result to unstable without having to use `script` or a framework specific publisher plugin (e.g. junit plugin)

          [JENKINS-56683] Declarative Pipeline: set the build result when tests fail without a script block

          dnusbaum does the new `unstable` step solve this? I think it still requires a `script` block, right?

          Jakub Bochenski added a comment - dnusbaum does the new `unstable` step solve this? I think it still requires a `script` block, right?

          Devin Nusbaum added a comment -

          jbochenski From a quick glance, you should be able to use warnError, which we added specifically so it could be used in Declarative without a script block:

          Change this code:

          script {
            try {
              echo 'Running tests...'
              sh 'exit 1'
            }
            catch (exc) {
              echo 'Testing failed!'
              currentBuild.result = 'UNSTABLE'
            }
          }
          

          to this:

          warnError('Testing Failed!') {
            echo 'Running tests...'
            sh 'exit 1'
          }
          

          Devin Nusbaum added a comment - jbochenski From a quick glance, you should be able to use warnError , which we added specifically so it could be used in Declarative without a script block: Change this code: script { try { echo 'Running tests...' sh 'exit 1' } catch (exc) { echo 'Testing failed!' currentBuild.result = 'UNSTABLE' } } to this: warnError( 'Testing Failed!' ) { echo 'Running tests...' sh 'exit 1' }

          That's great. I think we can resolve this issue now

          Jakub Bochenski added a comment - That's great. I think we can resolve this issue now

          dnusbaum Do we have something similar for failure as well?

          Gourav Pattnaik added a comment - dnusbaum Do we have something similar for failure as well?

          Devin Nusbaum added a comment -

          whitewolf123 Use catchError (documentation here) and specify the buildResult and/or stageResult as desired:

          catchError(message: 'foo', buildResult: 'FAILURE', stageResult: 'FAILURE') {
            ...
          }
          

          Devin Nusbaum added a comment - whitewolf123 Use catchError ( documentation here ) and specify the buildResult and/or stageResult as desired: catchError(message: 'foo' , buildResult: 'FAILURE' , stageResult: 'FAILURE' ) { ... }

            abayer Andrew Bayer
            jbochenski Jakub Bochenski
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: