web-dev-qa-db-ja.com

パイプラインジョブに渡すJenkins Choiceパラメーター

現在、このパラメーターの1つがChoiceパラメーターであるさまざまなパラメーターを持つパイプラインジョブがあります。そのジョブパラメータのconfig.xml出力は次のとおりです。

<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>

文字列パラメーターを渡すことで、パイプラインからこのジョブを呼び出すことができます:

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

しかし、選択パラメータのパラメータを定義する方法を得ることができませんでした:

私は次を試しました:

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

しかし、これは次のエラーで失敗しました。

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

質問は次のとおりです。他のパイプラインジョブを呼び出す際に選択パラメーターを定義する方法は次のとおりです。

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

誰かがそのようなことの例を持っていますか?

10
khmarbaise

私は以下の構文を使用する実用的な例を見てきました:

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

基本的に、特別な方法で選択パラメーターを扱わず、通常の文字列パラメーターとして扱います。

24
c3st7n

スクリプトモードでは、このようにすることもできます。現時点ではバグが発生し、選択パラメータは配列ではなく改行区切りの文字列であると想定されています。 PipelineとJenkinsFileをスクリプトモードで使用する場合、次のような簡単な修正を行うことができます。

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

これを最初のnodeステートメントの前に置いて、ビルドにパラメーターを追加できます。

更新:Jenkinsパイプラインファイルの拡張選択パラメータープラグインを使用した複数選択の例:

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
        ])
])
11
Arjan Schokking

C3st7nのヒントに基づいて、以下をテストしました。

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

  ]

これは動作します。

3
khmarbaise