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

Cannot get RestartDeclarativePipelineCause origin run number and stage name by using RunWrapper.getBuildCauses

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

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

        Attachments

          Activity

          agabrys Adam Gabryś created issue -
          agabrys Adam Gabryś made changes -
          Field Original Value New Value
          Description 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).
          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).
          Hide
          agabrys Adam Gabryś added a comment - - edited

          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:

          if (isRestartedFromStage('stageName')) {
              // do something
          }

          At this moment we created a utility method in our Jenkins library:

          import jenkins.model.Jenkins
          
          boolean call(def parameters = [:]) {
          	def cause = currentBuild.rawBuild.getCause(Class.forName(
          		'org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause',
          		true,
          		Jenkins.get().pluginManager.uberClassLoader)
          	)
          	// TODO: use below when https://issues.jenkins.io/browse/JENKINS-64428 is fixed
          	// def cause = currentBuild.getBuildCauses('org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause').find()
          
          	if (cause == null) {
          		return false
          	}
          
          	def stage = parameters instanceof Map ? parameters['stage'] : parameters
          	return stage == null || stage == cause.originStage
          }
          Show
          agabrys Adam Gabryś added a comment - - edited 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: if (isRestartedFromStage( 'stageName' )) { // do something } At this moment we created a utility method in our Jenkins library: import jenkins.model.Jenkins boolean call(def parameters = [:]) { def cause = currentBuild.rawBuild.getCause( Class .forName( 'org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause' , true , Jenkins.get().pluginManager.uberClassLoader) ) // TODO: use below when https://issues.jenkins.io/browse/JENKINS-64428 is fixed // def cause = currentBuild.getBuildCauses( 'org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause' ).find() if (cause == null ) { return false } def stage = parameters instanceof Map ? parameters[ 'stage' ] : parameters return stage == null || stage == cause.originStage }

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            agabrys Adam Gabryś
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: