Jenkins 选择参数传递到管道作业

问题描述 投票:0回答:6

目前我有一个管道作业,它具有不同的参数,其中一个参数是 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: '??????', ????],
  ]

有人有这样的例子吗?

jenkins groovy jenkins-pipeline jenkins-workflow
6个回答
49
投票

我看到了一个使用以下语法的工作示例:

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

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


14
投票

在脚本模式下,你也可以这样做,目前它是有缺陷的,它期望选择参数作为换行符分隔的字符串而不是数组。 在脚本模式下使用 Pipeline 和 JenkinsFile 时,您可以进行如下快速修复:

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

您可以将其放在第一个节点语句之前,以将参数添加到您的构建中。

更新: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
        ])
])

8
投票

使用

ExtendedChoiceParameterValue
代替
ChoiceParameterValue

例如

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

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

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

任务的所有三个版本执行相同。请注意所使用的具体引号。


6
投票

根据 c3st7n 的提示,我测试了以下内容:

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

这有效。


2
投票

您可以使用

extendedChoice
代替
ChoiceParameterValue
喜欢:

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

詹金斯文档: https://www.jenkins.io/doc/pipeline/steps/pipeline-build-step/

© www.soinside.com 2019 - 2024. All rights reserved.