2
votes

I would like to retrieve the list of choices of my choice parameter in my job with the api jenkins

I try this :

http://jenkins-url/api/json?tree=jobs[name,builds[number,actions[parameters[name,value]]]]&pretty=true

But I get just value

"name": "context", "value": "foo"

I would this output :

"name": "context", "value": "foo, foo, ..."

Is it possible ?

2
the closest I got to getting only the choices was using: ?tree=actions[parameterDefinitions[choices]]RaamEE

2 Answers

0
votes

Be sure to check out the documentation for retrieving the build parameters for a given job https://javadoc.jenkins-ci.org/hudson/model/ParametersDefinitionProperty.html

You can follow the example given below if you already have a reference to a specific Jenkins job

def paramsProp = job.getProperty(ParametersDefinitionProperty.class)
if ( paramsProp ) {
    // Single out specific build param
    myBuildParam = paramsProp.getParameterDefinition( 'MY_BUILD_PARAM' )
    println 'Default value for ' + myBuildParam.getDisplayName() + ': ' + myBuildParam.getDefaultParameterValue().value
    println 'Param Type ' + myBuildParam.getClass()

    // Get all build params
    myBuildParams = paramsProp.getParameterDefinitions()
    // ... GET VALUES ETC
}

The specific items you can retrieve from the build parameter will be contingent on what that parameter type is. You'll need to look up the API for each, for example a ChoiceParameter

https://javadoc.jenkins.io/hudson/model/ChoiceParameterDefinition.html

0
votes

Here is an example of method one can use in Jenkins pipeline job:

private String[] getChoices {
   def job = hudson.model.Hudson.instance.getItem(env.JOB_NAME)
   def paramsProp = job.getProperty(ParametersDefinitionProperty.class)
   def releaseTeamParameter = paramsProp.getParameterDefinitions().find { it.name == "MY_CHOICES_PARAM_NAME"}       
   return releaseTeamParameter.getChoices().toArray()
}

this method is meant to be executed from within the job itself and you need to specify a parameter name in place of MY_CHOICES_PARAM_NAME