Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
eran meiri Avatar asked Feb 05 '23 05:02

eran meiri


1 Answers

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
like image 91
dan.goriaynov Avatar answered Feb 12 '23 11:02

dan.goriaynov