khmarbaise
khmarbaise

Reputation: 97349

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?

Upvotes: 26

Views: 139326

Answers (6)

khmarbaise
khmarbaise

Reputation: 97349

Based on the tip of c3st7n I have tested the following:

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

This works.

Upvotes: 6

Viktor Chernov
Viktor Chernov

Reputation: 191

You can use extendedChoice instead of ChoiceParameterValue like:

build job: 'NameOfTheJob', parameters: [extendedChoice(name: 'WHERE', value: 'F3')]

Jenkins docs: https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/

Upvotes: 2

Schlueter
Schlueter

Reputation: 4029

As documented at https://www.jenkins.io/doc/book/pipeline/syntax/#parameters in September 2020, the documented syntax to use a choice parameter in a pipeline is:

pipeline {
    agent any
    parameters {
        choice(
            name: 'CHOICE',
            choices: ['one', 'two', 'three'],
            description: ''
        )
    }
    stages {
        stage('Example') {
            steps {
                echo "Choice: ${params.CHOICE}"
                sh "echo Choice: ${params.CHOICE}"
                sh 'echo Choice: $CHOICE'
            }
        }
    }
}

In testing, the choice defaults to the first parameter in the list, I have not looked into if this has potential to be otherwise.

All three versions of the task perform the same. Note the specific quotes used.

Upvotes: 9

Digital_Reality
Digital_Reality

Reputation: 4738

Use ExtendedChoiceParameterValue instead of ChoiceParameterValue

e.g.

[$class: 'ExtendedChoiceParameterValue', name: 'param_name', value: 'param_value']

Upvotes: 8

Arjan Schokking
Arjan Schokking

Reputation: 251

In script mode you can also do it like this, at the moment it is bugged and it expects the choice parameters as a newline delimited string instead of an array. When using the Pipeline and JenkinsFile in script mode you can do a quick fix like follows:

properties(
    [parameters([choice(choices: ["A", "B", "C"].join("\n"),
    description: 'Some choice parameter', 
    name: 'SOME_CHOICE')])])

You can put this before your first node statement to add parameters to your builds.

Update: example multi select with extended choice parameter plugin in Jenkins pipeline file:

com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition extendedChoiceParameterDefinition = new com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition(
    "OPTION_NAME", // name
    "PT_CHECKBOX", // type
    "option1,option2,option3", // values
    null, // projectName
    null, // propertyFile
    null, // groovyScript
    null, // groovyScriptFile
    null, // bindings
    null, // groovyClasspath
    null, // propertyKey
    "option1,option2", // defaultValue
    null, // defaultPropertyFile
    null, // defaultGroovyScript
    null, // defaultGroovyScriptFile
    null, // defaultBindings
    null, // defaultGroovyClasspath
    null, // defaultPropertyKey
    null, // descriptionPropertyValue
    null, // descriptionPropertyFile
    null, // descriptionGroovyScript
    null, // descriptionGroovyScriptFile
    null, // descriptionBindings
    null, // descriptionGroovyClasspath
    null, // descriptionPropertyKey
    null, // javascriptFile
    null, // javascript
    false, // saveJSONParameterToFile
    false, // quoteValue
    10, // visible item count
    "Select your options", // description
    "," //multiSelectDelimiter
)

normalChoiceOptions = ["option1","option2"]

properties([
        parameters([
                choice(choices: normalChoiceOptions.join("\n"), description: 'Single select option', name: 'SOME_OPTION'),                
                extendedChoiceParameterDefinition
        ])
])

Upvotes: 14

c3st7n
c3st7n

Reputation: 1961

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.

Upvotes: 50

Related Questions