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

*爱你&永不变心* 提交于 2019-12-22 06:58:44

问题


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.


回答1:


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
}



回答2:


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


来源:https://stackoverflow.com/questions/31394647/how-to-access-list-of-jenkins-job-parameters-from-within-a-jobdsl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!