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

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

    Details

    • Similar Issues:

      Description

      
      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)

        Attachments

          Issue Links

            Activity

            Hide
            jbochenski Jakub Bochenski added a comment -

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

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

            Jakub Bochenski 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'
            }
            
            Show
            dnusbaum Devin Nusbaum added a comment - Jakub Bochenski 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' }
            Hide
            jbochenski Jakub Bochenski added a comment -

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

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

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                jbochenski Jakub Bochenski
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: