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

Add env variable LAST_STAGE_NAME which will persist when stage ends prematurely

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • Jenkins 2.73.3
      pipeline-stage-step-plugin 2.3

      Hi,

      The environment STAGE_NAME variable does not persist when the stage ends prematurely. here's an example:

       

      node('windows') {
          try {
              stage('MY STAGE') {
                  echo "before=" + env.STAGE_NAME
                  error('something bad happened')
                  echo "after=" + env.STAGE_NAME
              } 
          } catch (def e) {
              echo "after failure="+env.STAGE_NAME
              throw e
          }
      }

      console output:

      Started by user admin
      [Pipeline] node
      Running on SLAVE01 in F:\jenkins-remote\workspace\stagename
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (MY STAGE)
      [Pipeline] echo
      before=MY STAGE
      [Pipeline] error
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] echo
      after failure=null
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      ERROR: something bad happened
      Finished: FAILURE
      

      While it makes sense for STAGE_NAME to be null when not in an actual stage, it would be nice if we had a build property or environment variable such as LAST_STAGE_NAME which would keep the last stage executed. This functionality is critical to my shared library logic in which I have a generic script runner and I want to know at which stage the script it ran failed (without having to modify each flow).

      It would also be useful if this property would be available when calling getBuildVariables() on a downstream job started by the build step.

            Unassigned Unassigned
            pyrocks Mor L
            Votes:
            28 Vote for this issue
            Watchers:
            19 Start watching this issue

              Created:
              Updated: