Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access list of Jenkins job parameters from within a JobDSL script?

I would like to save the parameters passed into a JobDSL job. I know I can refer to individual parameters but I would like to make the code generic. How would I access the list of parameters passed to the job?

The current code looks something like:

final jobParameters = new File('parameters')
jobParameters.write("""
    |AOEU=${AOEU}
    |SNTH=${SNTH}
"""[1..-1].stripMargin().trim())

I would like to be able to get it to look something like:

final jobParameters = new File('parameters')
jobParameters.write(params.iterator().join('\n'))

params is something that's available in the Build Flow Plugin but not the JobDSL Plugin.

like image 901
Noel Yap Avatar asked Jul 13 '15 22:07

Noel Yap


People also ask

What is DSL in Jenkins pipeline?

DSL stands for Domain Specific Language. You can describe your jobs in Jenkins using a Groovy Based Language. Groovy-- It's similar to java but simpler because it's much more dynamic. It''s Scripting Language. Jenkins job DSL plugin was designed to make it easier to manage jobs.

What is a seed Job in Jenkins?

The seed job is a normal Jenkins job that runs the Job DSL script; in turn, the script contains instructions that create additional jobs. In short, the seed job is a job that creates more jobs. In this step, you will construct a Job DSL script and incorporate it into a seed job.

How to Install Job DSL plugin?

Getting Started. First, start a Jenkins instance with the Job DSL plugin installed. Then create a freestyle project named "seed". Add a "Process Job DSLs" build step and paste the script below into the "DSL Script" field.


2 Answers

The DSL does not offer access to the build parameters. But the script has access to the Jenkins object model, so you can use the Jenkins API to retrieve the current build and its parameters:

import hudson.model.*

Build build = Executor.currentExecutor().currentExecutable
ParametersAction parametersAction = build.getAction(ParametersAction)
parametersAction.parameters.each { ParameterValue v ->
    println v
}
like image 83
daspilker Avatar answered Sep 27 '22 20:09

daspilker


This is how I do it for debugging (I read it somewhere, I'm by no means a Groovy or Job DSL expert...):

binding.variables.each {
  println "${it.key} = ${it.value}"
}

This shows all the existing environment variables, including the job parameters.

JOB_NAME = job-generator
...
NEXT_PROJECTS = baz,bat
...
PROJECT_TYPE = Software
...
PROJECTS = foo,bar
...
SHELL = /bin/bash
like image 45
eerriicc Avatar answered Sep 27 '22 21:09

eerriicc