-
Bug
-
Resolution: Unresolved
-
Major
-
None
-
Pipeline: Declarative - 1.7.2
The org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause doesn't set @Exported visibility for the getOriginRunNumber and getOriginStage methods, so it is impossible to read values by using Jenkins declarative API: currentBuild.getBuildCauses().
pipeline { agent { label 'tiny' } stages { stage('One') { steps { echo 'one' } } stage('Two') { steps { echo 'two' script { currentBuild.getBuildCauses().each { echo "${it}" } } } } } }
Output when restarted from Stage:
[...] 12:02:38 [Pipeline] echo 12:02:38 [_class:hudson.model.Cause$UserIdCause, shortDescription:Started by user Gabrys, Adam, userId:SECRET, userName:Gabrys, Adam] 12:02:38 [Pipeline] echo 12:02:38 [_class:org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause, shortDescription:Restarted from build #13, stage One] [...]
As you see Cause$UserIdCause exported all fields and RestartDeclarativePipelineCause only the short description (I don't want to parse string which btw. could be localized).
[JENKINS-64428] Cannot get RestartDeclarativePipelineCause origin run number and stage name by using RunWrapper.getBuildCauses
Description |
Original:
The [org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause|https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.7.2/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/causes/RestartDeclarativePipelineCause.java] doesn't set [@Exported|https://stapler.kohsuke.org/apidocs/org/kohsuke/stapler/export/Exported.html] visibility for the {{getOriginRunNumber}} and {{getOriginStage}} methods, so it is impossible to read values by using Jenkins declarative API: {{currentBuild.getBuildCauses()}}.
{code} pipeline { agent { label 'tiny' } stages { stage('One') { steps { echo 'one' } } stage('Two') { steps { echo 'two' script { currentBuild.getBuildCauses().each { echo "${it}" } } } } } } {code} Output when restarted from Stage: {noformat} [...] 12:02:38 [Pipeline] echo 12:02:38 [_class:hudson.model.Cause$UserIdCause, shortDescription:Started by user Gabrys, Adam, userId:SECRET, userName:Gabrys, Adam] 12:02:38 [Pipeline] echo 12:02:38 [_class:org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause, shortDescription:Restarted from build #13, stage One] [...] {noformat} As you see {{Cause$UserIdCause}} exported all fields and {{RestartDeclarativePipelineCause}} only description (I don't want to parse string which btw. could be localized). |
New:
The [org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause|https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.7.2/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/causes/RestartDeclarativePipelineCause.java] doesn't set [@Exported|https://stapler.kohsuke.org/apidocs/org/kohsuke/stapler/export/Exported.html] visibility for the {{getOriginRunNumber}} and {{getOriginStage}} methods, so it is impossible to read values by using Jenkins declarative API: {{currentBuild.getBuildCauses()}}.
{code} pipeline { agent { label 'tiny' } stages { stage('One') { steps { echo 'one' } } stage('Two') { steps { echo 'two' script { currentBuild.getBuildCauses().each { echo "${it}" } } } } } } {code} Output when restarted from Stage: {noformat} [...] 12:02:38 [Pipeline] echo 12:02:38 [_class:hudson.model.Cause$UserIdCause, shortDescription:Started by user Gabrys, Adam, userId:SECRET, userName:Gabrys, Adam] 12:02:38 [Pipeline] echo 12:02:38 [_class:org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause, shortDescription:Restarted from build #13, stage One] [...] {noformat} As you see {{Cause$UserIdCause}} exported all fields and {{RestartDeclarativePipelineCause}} only the short description (I don't want to parse string which btw. could be localized). |
We require access to the origin stage name to fully support the Restart from Stage functionality. Sometimes some mandatory steps were executed in previous stages and we have to fetch required data if missing:
At this moment we created a utility method in our Jenkins library: