How to pass choice parameter to call a job inside jenkins pipeline

て烟熏妆下的殇ゞ 提交于 2020-02-07 02:26:11

问题


How can I pass choice parameters for the downstream job when called inside a stage in the jenkins pipeline?

I tried the below solutions but none worked:

stage('build job') {
    steps{
        script{
              build job: 'test',
                   parameters: [
                                choice(choices: "option1\noption2\noption3\n", description: '', name: 'choiceParam')
                                ] 
              }
     }
}

fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is using symbol ‘choice’

Tried these as well:

 parameters:
   [
     [$class: 'ChoiceParameterValue', name: 'choiceParam', value: "1\n\2\n3\n"],
   ]

fails with java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue

I actually want to pass the choice parameter as a build parameter like "$choiceParam" for value so that I can just update the jenkins job configuration instead of always updating the values in the pipeline script

Can someone please help me with this

Thanks


回答1:


When you are building a job via the Build step, you are kicking it off so you need to have "selected" a value.

In this instance you would pass in the desired 'String' choice. Not a list of choices. i.e. "1"

We create our list of params and then pass that in. So: our current job has these input params:

choice(name: 'ENV', choices: product, description: 'Env'),
choice(name: 'ENV_NO', choices: envParams(product), description: 'Env No'),

We pass these downstream by setting them:

List<ParameterValue> newParams = [
    new StringParameterValue('ENV', params.ENV),
    new StringParameterValue('ENV_NO', params.ENV_NO),
]
build(job: "job", parameters: newParams, propagate: false)


来源:https://stackoverflow.com/questions/53732217/how-to-pass-choice-parameter-to-call-a-job-inside-jenkins-pipeline

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