eran meiri
eran meiri

Reputation: 1508

extract parameters from a jenkins previous build

I am working on Jenkins version 2.32.1 pipeline. I want to extract the parameters that were chosen in the previous build of my job.

In our previous Jenkins instance ( 1.627 ) we were using jenkins.model.Jenkins.instance.getItem(job).lastBuild.getBuildVariables().get(param);

For some reason this is not working in this version (I also tried disabling the sandbox).

Any pointers on how to accomplish it?

Upvotes: 5

Views: 15900

Answers (4)

JavaTechnical
JavaTechnical

Reputation: 9347

For those who are not able to access getActions() due to admin permissions i.e. facing the following error:

Scripts not permitted to use method hudson.model.Actionable getActions

They can copy the parameter variables to the env and get them using build.previousBuild.buildVariables

stage('Prepare environment') {
  steps {
    script {
       env.MY_PARAM_COPY = "${MY_PARAM}"
    }
  }
}
println("MY_PARAM in previous build: ${currentBuild.previousBuild.buildVariables["MY_PARAM_COPY"]}")

Upvotes: 1

Vincent Baronnet
Vincent Baronnet

Reputation: 101

That's how I made it works, answer from @dan.goriaynov and @jherb caused some CPS closure issues for me.

(the gist of the code is to allow only greater TAG number than the previous one to be deployed)

stage('Validate build number') {
  def previous_build = currentBuild.getPreviousBuild().getRawBuild();
  def PREVIOUS_TAG = '';
  for (int i = 0; i < previous_build.allActions.size(); i++) {
    if (previous_build.allActions[i] in hudson.model.ParametersAction) {
      PREVIOUS_TAG = previous_build.allActions[i].getParameter("TAG").value
    }
  }
  if  (PREVIOUS_TAG.toInteger() > TAG.toInteger()) {
    echo PREVIOUS_TAG
    error('TAG number needs to be greater than the previous one')
  }
}

Upvotes: 0

dan.goriaynov
dan.goriaynov

Reputation: 301

Simplified version of the previous script:

def build = Jenkins.get().getItems(org.jenkinsci.plugins.workflow.job.WorkflowJob).find {it.displayName == 'YOUR_JOB_NAME_HERE'}?.getLastBuild()
build.actions.find{ it instanceof ParametersAction }?.parameters.each {echo "${it.name}=${it.value}"}

Actually a little bit shorter version for those who want to get the params for the current build from the previous run and is working on new 2+ Jenkins versions.
To get 1 particular parameter:

def cls = currentBuild.getPreviousBuild().getRawBuild().actions.find{ it instanceof ParametersAction }?.parameters.find{it.name == 'cls'}?.value

Get all params respectfully:

def cls = currentBuild.getPreviousBuild().getRawBuild().actions.find{ it instanceof ParametersAction }?.parameters

Upvotes: 9

jherb
jherb

Reputation: 402

Something like this might work, based on https://stackoverflow.com/a/19564602/3920342:

def h = hudson.model.Hudson.instance
def r = null
h.getItems(org.jenkinsci.plugins.workflow.job.WorkflowJob).each {project ->
    if (project.displayName.equals('YOUR_JOB_NAME')) {
        r = project
    }
}
r.getBuilds().findAll { b -> // here we loop over all past builds, apply some filter if necessary
    def p = b?.actions.find{ it instanceof ParametersAction }?.parameters
    p.each {
        echo "parameter ${it.name}: ${it.value}"
    }
}

Upvotes: 3

Related Questions