Search code examples
jenkinsgroovyjenkins-pipelinejenkins-workflow

Jenkins Choice parameter Passing to a pipeline Job


Currently I have a pipeline job which has different paramters where one of this parameters is a Choice parameter. Here is the config.xml output of that job parameter:

<hudson.model.ChoiceParameterDefinition>
    <choices class="java.util.Arrays$ArrayList">
        <a class="string-array">
            <string>f1</string>
            <string>f2</string>
            <string>f3</string>
            <string>f4</string>
        </a>
    </choices>
    <name>WHERE</name>
    <description>Something</description>
</hudson.model.ChoiceParameterDefinition>

Now I can call this job from a pipeline via by passing a string parameter:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
  ]

But I couldn't get a way to define the parameters for a choice parameter:

I have tried the following:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
    [$class: 'ChoiceParameterValue', name: 'WHERE', value: 'F3'],
  ]

But this failed with the following error:

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

So the question is: How to define a choice parameters in calling an other pipeline job:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
    [$class: '??????', ????],
  ]

Does someone has an example of such thing?


Solution

  • I have seen a working example that uses the below syntax:

    build job:'NameOfTheJob', parameters: [
          string(name: 'FirstOption', value: "test"),
          string(name: 'AnotherOption', value: "test12")
    ]
    

    Basically, don't treat the choice parameters in a special manner, just treat them as regular string parameters.