Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve Jenkins build parameters using the Groovy API?

Tags:

jenkins

groovy

I have a parameterized job that uses the Perforce plugin and would like to retrieve the build parameters/properties as well as the p4.change property that's set by the Perforce plugin.

How do I retrieve these properties with the Jenkins Groovy API?

like image 516
Noel Yap Avatar asked Jun 04 '12 13:06

Noel Yap


People also ask

How do I get Jenkins build parameters?

Defining Build Parameters Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do I get parameters in Jenkins pipeline?

You can generate the parameter pipeline code block easily using the Jenkins pipeline generator. You will find the Pipeline syntax generator link under all the pipeline jobs, as shown in the image below. Navigate to the pipeline generator in Jenkins and under steps, search for properties, as shown below.

How do I run a Groovy script in Jenkins build?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name.


2 Answers

Update: Jenkins 2.x solution:

With Jenkins 2 pipeline dsl, you can directly access any parameter with the trivial syntax based on the params (Map) built-in:

echo " FOOBAR value: ${params.'FOOBAR'}"

The returned value will be a String or a boolean depending on the Parameter type itself. The syntax is the same for scripted or declarative syntax. More info at: https://jenkins.io/doc/book/pipeline/jenkinsfile/#handling-parameters

If your parameter name is itself in a variable:

def paramName = "FOOBAR" def paramValue = params.get(paramName) // or: params."${paramName}" echo """ FOOBAR value: ${paramValue}" 

Original Answer for Jenkins 1.x:

For Jenkins 1.x, the syntax is based on the build.buildVariableResolver built-ins:

// ... or if you want the parameter by name ... def hardcoded_param = "FOOBAR" def resolver = build.buildVariableResolver def hardcoded_param_value = resolver.resolve(hardcoded_param) 

Please note the official Jenkins Wiki page covers this in more details as well, especially how to iterate upon the build parameters: https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script

The salient part is reproduced below:

// get parameters def parameters = build?.actions.find{ it instanceof ParametersAction }?.parameters parameters.each {    println "parameter ${it.name}:"    println it.dump() } 
like image 71
Patrice M. Avatar answered Sep 17 '22 21:09

Patrice M.


For resolving a single parameter (I guess what's most commonly needed), this is the simplest I found:

build.buildVariableResolver.resolve("myparameter")

in your Groovy System script build step.

like image 21
inger Avatar answered Sep 20 '22 21:09

inger