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

An additional final stage is required to make previous stage fail when it should

XMLWordPrintable

      If I have a single stage that calls an sh script, and that script fails, and I use try..catch, then this stage is marked as green success in the pipeline view, but it should be marked as a failure. I can "fix" this by adding an additional final stage at the end of the Jenkinsfile and then the first stage (previous stage) will correctly be marked as failed.

      Here is some simple Jenkinsfile test code that shows how to reproduce this bug:

      #!groovy

      def machine1 = 'mymachine'

      node (machine1) {

      currentBuild.result='SUCCESS'

      stage "Stage One"

      try

      { sh "banana" }

      catch (err) {
      echo "Stage One Failed: ${err}"
      currentBuild.result='FAILURE'
      }

      //
      // We need this final stage to mark the end of the preceding stage,
      // so that the preceding stage's code will fail if its sh script fails
      //
      // stage "End"

      echo "We've reached the end of the '${env.JOB_NAME}' pipeline. The build has finished."

      }

      Simply uncomment the penultimate line that says stage "End" and you'll see what I mean.

            svanoort Sam Van Oort
            will177 Will Berriss
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: