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

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

XMLWordPrintable

      If I have a single stage pipeline that calls an sh script that deliberately fails, and I use try .. catch around the script, then the stage is marked as "green/success" in the pipeline, but it should be marked as "pink/failed."
      However, if I then add a final additional stage, the original first stage (previous stage) will then correctly be marked as "pink/failed."

      Here is some simple Jenkinsfile code that demonstrates the problem.

      
      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."
      
      }
      
      

      You will see that the first (and only) stage is marked as green passed.
      If you uncomment the penultimate line stage "End" then you will find the first stage to be marked as pink failed, which is correct.

      Thanks

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

              Created:
              Updated:
              Resolved: