Reputation: 8200
In a Jenkins pipeline, it is possible to request input data using
def returnValue = input message: 'Need some input',
parameters: [string(defaultValue: 'adefval', description: 'a', name: 'aaa'),
string(defaultValue: 'bdefval', description: 'b', name: 'bbb')]
To build such a list dynamically, I tried something like
def list = ["foo","bar"]
def inputRequest = list.collect { string(defaultValue: "def", description: 'description', name: it) }
def returnValue = input message: 'Need some input', parameters: [inputRequest]
This does not work:
java.lang.ClassCastException: class org.jenkinsci.plugins.workflow.support.steps.input.InputStep.setParameters() expects class hudson.model.ParameterDefinition but received class java.util.ArrayList
Probably, Groovy can figure out dynamically in the first case which object is required, but in the second case it does not work anymore, as collect returns an ArrayList?
How to create such a list correctly?
edit: maybe this question not very useful for itself, but may still serve as a code sample...
Upvotes: 3
Views: 4484
Reputation: 833
I struggled with this for a while, but it's not as complicated as I thought.
At its simplest, you can define an empty array and fill it with parameter types. I'm honestly not clear on what the object type is, but it works.
ArrayList pa = []
if(<some conditional>) {
pa += choice (name: 'source_feed', choices: ['Development', 'Production'])
pa += string (name: 'deployVersion', defaultValue: 'Latest', description: 'version')
pa += extendedChoice (name: 'environments', value: 'one,two,three', description: 'envs', type: 'PT_CHECKBOX')
pa += booleanParam (name: 'dryRunMode', defaultValue: false, description: 'dry')
pa += booleanParam (name: 'skipPreChecks', defaultValue: false, description: 'skip')
}
else if (<some other conditional>) {
pa += string (name: 'CommandServiceVersion', defaultValue: '', description: 'commandservice')
}
def result = input message: "Enter Parameters",
ok: 'ok go!',
parameters: pa
echo result.toString()
As long as you can pass the Array[]
around, it works. I ended up creating the array[]
outside the pipeline, as a global, so it can be passed around throughout the run; as opposed to an env var, which can only be a string.
Upvotes: 0
Reputation: 69
You can use the the following code. It will generate the choices A,B,C for you. This can be found by using the Pipeline syntax option in Jenkins. I use it because it saves time and less typos.
def createInputParameters(){
properties([[$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled:
false],parameters([choice(choices: ['A', 'B', 'C'], description: '', name:
'Test')]),
[$class: 'ThrottleJobProperty', categories: [],
limitOneJobWithMatchingParams: false,
maxConcurrentPerNode: 0, maxConcurrentTotal: 0,
paramsToUseForLimit: '',
throttleEnabled: false,
throttleOption: 'project']])
}
Upvotes: -1
Reputation: 8200
Ok it was quite a simple fix, as the collect already returns an ArrayList
, it should not be wrapped into another list when setting the parameters...
def returnValue = input message: 'Need some input', parameters: inputRequest
Upvotes: 4