Jenkins Choice参数传递给管道作业

khm*_*ise 12 groovy jenkins jenkins-workflow jenkins-pipeline

目前我有一个管道作业,它有不同的参数,其中一个参数是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>
Run Code Online (Sandbox Code Playgroud)

现在我可以通过传递字符串参数从管道调用此作业:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
  ]
Run Code Online (Sandbox Code Playgroud)

但我无法找到一种方法来定义选择参数的参数:

我尝试过以下方法:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
    [$class: 'ChoiceParameterValue', name: 'WHERE', value: 'F3'],
  ]
Run Code Online (Sandbox Code Playgroud)

但是由于以下错误而失败:

java.lang.UnsupportedOperationException: no known implementation of class hudson.model.ParameterValue is named ChoiceParameterValue
Run Code Online (Sandbox Code Playgroud)

所以问题是:如何在调用其他管道作业时定义选择参数:

build job: "NameOfTheJob"", parameters:
  [
    [$class: 'StringParameterValue', name: 'BRANCH', value: "${BRANCH}"],
    [$class: '??????', ????],
  ]
Run Code Online (Sandbox Code Playgroud)

有人有这样的例子吗?

c3s*_*t7n 26

我见过一个使用以下语法的工作示例:

build job:'NameOfTheJob', parameters: [
      string(name: 'FirstOption', value: "test"),
      string(name: 'AnotherOption', value: "test12")
]
Run Code Online (Sandbox Code Playgroud)

基本上,不要以特殊方式处理选择参数,只需将它们视为常规字符串参数即可.


Arj*_*ing 11

在脚本模式下,你也可以像这样做,在它被窃听的时候,它期望选择参数作为换行符分隔的字符串而不是数组.在脚本模式下使用Pipeline和JenkinsFile时,您可以执行以下快速修复:

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

您可以在第一个节点语句之前添加此参数,以便为构建添加参数.

更新:在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
        ])
])
Run Code Online (Sandbox Code Playgroud)


Dig*_*ity 8

使用ExtendedChoiceParameterValue而不是ChoiceParameterValue

例如

[$class: 'ExtendedChoiceParameterValue', name: 'param_name', value: 'param_value']
Run Code Online (Sandbox Code Playgroud)


bsc*_*ter 8

正如 2020 年 9 月https://www.jenkins.io/doc/book/pipeline/syntax/#parameters中记录的那样,在管道中使用选择参数的记录语法是:

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'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试中,选择默认为列表中的第一个参数,我还没有研究是否有可能是其他情况。

该任务的所有三个版本执行相同的操作。请注意所使用的具体报价。


khm*_*ise 6

基于 c3st7n 的提示,我测试了以下内容:

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

  ]
Run Code Online (Sandbox Code Playgroud)

这有效。