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

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

      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

          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
          }

          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 }

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

              Created:
              Updated: