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

Provide a way to access a stage name from within the stage and its contents

      Please provide a way to access the stage name from within the stage and its contents (like post).

      Background

      I have a multibranch declarative script. I am struggling with the limitation of one JenkinsFile per branch. I want to run various tasks (i.e. stages) on the branch (e.g. build several separate apps) and have a clear indication of which stage failed in the email notification subject sent on failure. I assume that stage processing will stop as soon as one stage fails. The post clause can then call the email-ext plugin and specify in Subject the stage that failed. Developers will then see quickly which task failed, rather than having to look through the console log (or email body).

          [JENKINS-44456] Provide a way to access a stage name from within the stage and its contents

          Andrew Bayer added a comment -

          Actually moving this to pipeline-stage-step-plugin for a more general solution. It'll still need a followup in Declarative for JENKINS-41334 and the special casing of emulating stages via parallel branches, but that's trivial.

          Andrew Bayer added a comment - Actually moving this to pipeline-stage-step-plugin for a more general solution. It'll still need a followup in Declarative for JENKINS-41334 and the special casing of emulating stages via parallel branches, but that's trivial.

          Andrew Bayer added a comment -

          Initial PR in pipeline-stage-step up at https://github.com/jenkinsci/pipeline-stage-step-plugin/pull/10 - a little followup in Declarative still to come.

          Andrew Bayer added a comment - Initial PR in pipeline-stage-step up at https://github.com/jenkinsci/pipeline-stage-step-plugin/pull/10 - a little followup in Declarative still to come.

          Andrew Bayer added a comment -

          Added explicit setting of STAGE_NAME for parallel "stages" in Declarative over in https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/152.

          Andrew Bayer added a comment - Added explicit setting of STAGE_NAME for parallel "stages" in Declarative over in https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/152 .

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          pom.xml
          src/main/java/org/jenkinsci/plugins/workflow/support/steps/StageStepExecution.java
          src/test/java/org/jenkinsci/plugins/workflow/support/steps/StageStepTest.java
          http://jenkins-ci.org/commit/pipeline-stage-step-plugin/2e7f175432c014945de3bc6a8941733cb370541c
          Log:
          [FIXED JENKINS-44456] Add STAGE_NAME to environment

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pom.xml src/main/java/org/jenkinsci/plugins/workflow/support/steps/StageStepExecution.java src/test/java/org/jenkinsci/plugins/workflow/support/steps/StageStepTest.java http://jenkins-ci.org/commit/pipeline-stage-step-plugin/2e7f175432c014945de3bc6a8941733cb370541c Log: [FIXED JENKINS-44456] Add STAGE_NAME to environment

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy
          pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java
          pipeline-model-definition/src/test/resources/parallelStagesHaveStageName.groovy
          pom.xml
          http://jenkins-ci.org/commit/pipeline-model-definition-plugin/9e729c47b87333ebc96dd38f7597298cbfc9b831
          Log:
          JENKINS-44456 Set STAGE_NAME on parallel stages.

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java pipeline-model-definition/src/test/resources/parallelStagesHaveStageName.groovy pom.xml http://jenkins-ci.org/commit/pipeline-model-definition-plugin/9e729c47b87333ebc96dd38f7597298cbfc9b831 Log: JENKINS-44456 Set STAGE_NAME on parallel stages.

          How can we access this variable now in declarative pipelines? Is this functionality already published in Jenkins? It would be really useful to have this for sending notifications in case of build failures.

          Martin Hausler added a comment - How can we access this variable now in declarative pipelines? Is this functionality already published in Jenkins? It would be really useful to have this for sending notifications in case of build failures.

          martin_haeusler According to Github, it's not in the currently released version of the plugin. I've opened an issue (JENKINS-45713) to ask about a new release, keep an eye on that.

          Zalan Meggyesi added a comment - martin_haeusler According to Github, it's not in the currently released version of the plugin. I've opened an issue ( JENKINS-45713 ) to ask about a new release, keep an eye on that.

          Andrew Bayer added a comment -

          finally released as 2.3 =)

          Andrew Bayer added a comment - finally released as 2.3 =)

          David Aldrich added a comment -

          abayer Thanks for releasing this change.  Where can I find out what the related env variable is, and perhaps an example of how to use it?

          David Aldrich added a comment - abayer Thanks for releasing this change.  Where can I find out what the related env variable is, and perhaps an example of how to use it?

          abayer Many thanks, Andrew!

          Zalan Meggyesi added a comment - abayer Many thanks, Andrew!

          Mor L added a comment - - edited

          This is SO very useful (I was about to implement a wrapper to the stage step which does exactly that) - thanks.

          Mor L added a comment - - edited This is SO very useful (I was about to implement a wrapper to the stage step which does exactly that) - thanks.

          Robert Rotaru added a comment - - edited

          The wiki still shows 2.2 as the latest release version, despite the README below also showing 2.3 as available (and archives also showing 2.3 as available).

          https://wiki.jenkins.io/display/JENKINS/Pipeline+Stage+Step+Plugin

          Is there a reason for this?

          Robert Rotaru added a comment - - edited The wiki still shows 2.2 as the latest release version, despite the README below also showing 2.3 as available (and archives also showing 2.3 as available). https://wiki.jenkins.io/display/JENKINS/Pipeline+Stage+Step+Plugin Is there a reason for this?

          Jessie Ye added a comment -

          Is STAGE_NAME supported for parallel branches in scripted pipeline?

           

          Jessie Ye added a comment - Is STAGE_NAME supported for parallel branches in scripted pipeline?  

          Frank Escobar added a comment - - edited

          It would be better if we can get the current stage name and the RESULT. **On that way we can take decisions based on current stage result in the post conditions.

          Frank Escobar added a comment - - edited It would be better if we can get the current stage name and the RESULT . **On that way we can take decisions based on current stage result in the post conditions.

          Kevin Burnett added a comment -

          thanks for rocking this out. i was very excited that it was going to solve my use case, but i don't think it has yet. when I access STAGE_NAME from within the post block in my shared library, it's always yielding "Declarative: Post Actions," which is not what I need. I'd like to know if the build failed during the "commit" stage, "acceptance_tests", "deploy", etc. The person who answered this stackoverflow question seems to be having the same issue, and they worked around it by manually setting a variable for stage and then referencing their own variable. This is the sort of duplication I was hoping to avoid. does this use case deserve another jira issue?

          Kevin Burnett added a comment - thanks for rocking this out. i was very excited that it was going to solve my use case, but i don't think it has yet. when I access STAGE_NAME from within the post block in my shared library, it's always yielding "Declarative: Post Actions," which is not what I need. I'd like to know if the build failed during the "commit" stage, "acceptance_tests", "deploy", etc. The person who answered this stackoverflow question seems to be having the same issue, and they worked around it by manually setting a variable for stage and then referencing their own variable. This is the sort of duplication I was hoping to avoid. does this use case deserve another jira issue?

            abayer Andrew Bayer
            davida2009 David Aldrich
            Votes:
            0 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved: