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

Add env variable LAST_STAGE_NAME which will persist when stage ends prematurely

    • 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.

          [JENKINS-48315] Add env variable LAST_STAGE_NAME which will persist when stage ends prematurely

          Mor L created issue -
          Mor L made changes -
          Link New: This issue is related to JENKINS-44456 [ JENKINS-44456 ]
          Mor L made changes -
          Issue Type Original: Bug [ 1 ] New: Improvement [ 4 ]
          Mor L made changes -
          Summary Original: env variable STAGE_NAME does not persist when stage ends prematurely New: Add env variable LAST_STAGE_NAME which will persist when stage ends prematurely
          Mor L made changes -
          Description Original: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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.
          New: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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 which would keep the last stage. 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.
          Mor L made changes -
          Description Original: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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 which would keep the last stage. 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.
          New: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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. 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).
          Mor L made changes -
          Description Original: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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. 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).
          New: Hi,

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

           
          {code:java}
          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
              }
          }{code}
          console output:
          {code:java}
          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
          {code}
          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.
          Rudolf-Walter Kiss-Szakacs made changes -
          Link New: This issue relates to JENKINS-47236 [ JENKINS-47236 ]

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

              Created:
              Updated: