我该如何在Jenkins管道脚本中使用"Extended Choice Parameter"插件?

31

Extended Choice Parameter插件非常好用,我在通过UI配置的任务中使用它。 https://wiki.jenkins-ci.org/display/JENKINS/Extended+Choice+Parameter+plugin

然而,在Jenkinsfile风格的管道脚本中,我遇到了困难。由于Jenkins pipeline-syntax生成器创建了以下代码片段,因此似乎Extended Choice Parameter插件尚未完全兼容Pipeline脚本:

parameters([<object of type com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition>])

如果我手动创建参数,我会得到与https://issues.jenkins-ci.org/browse/JENKINS-32188中提到的相同行为。

org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class 

有没有人知道任何解决 ExtendedChoiceParameterDefinition 没有使用 @DataBoundConstructor 问题的方法?

  • Jenkins 2.19.2
  • Extended Choice Parameter plugin 0.75

4
JENKINS-34617 是一个开放的问题。 - mkobit
6个回答

28

自2019年4月2日起,由于此提交:https://github.com/jenkinsci/extended-choice-parameter-plugin/pull/25,现在可以这样使用:

例如,您可以像这样使用它:

properties([
    parameters([
        extendedChoice( 
            name: 'PROJECT', 
            defaultValue: '', 
            description: 'Sélectionnez le projet à construire.', 
            type: 'PT_SINGLE_SELECT', 
            groovyScript: valueKeysScript,
            descriptionGroovyScript: valueNamesScript
        )
    ])
])

如果你想知道每个可能的参数,你需要参考源代码。 如果你想知道"type"键的每个可能值,看一下PT_*常量


嗨 Dev,我想找一种绑定一组变量的方法,但是一直失败。有什么想法吗?谢谢。 - albertsha
@BigAlbert 你应该发布一个新问题并把链接发给我,这样我可以更容易地帮助你。 - DevAntoine
嗨,开发者,谢谢你的回复 - 我找到了问题 - 是我的错。 - albertsha
1
“valueKeysScript” 可以在哪里定义?或者整个 Groovy 代码需要内联吗?我尝试在 Jenkinsfile 中 pipeline 上面定义一个函数,但是在运行时找不到它,也许这在声明性流水线中不起作用? - NeilS

7

多选示例:https://support.cloudbees.com/hc/en-us/articles/115003895271-How-to-do-a-multiselect-input-in-a-pipeline - qwerty

6

打开http://jenkins-url.com/pipeline-syntax

在“步骤示例”下拉列表中选择“属性:设置作业属性”。

有一个“此项目已参数化”的复选框,然后您可以选择添加参数 > 扩展选择参数。在那里添加菜单项,然后单击“生成流水线脚本”进行转换。

裁剪它以删除“properties([parameters(['”之前和“]')])”之后的内容:

extendedChoice(defaultValue: 'whatif', description: 'Run as what if?', multiSelectDelimiter: ',', name: 'whatif', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', value: 'whatif, LIVE', visibleItemCount: 2)

enter image description here


'Generate Pipeline Script' 可以在哪里找到? - Dave
2
如果您的Jenkins服务器位于端口12345上的10.10.10.10,则URL为10.10.10.10:12345/pipeline-syntax。然后,在示例步骤下拉菜单中选择“属性:设置作业属性”。有一个复选框,“此项目已参数化”,然后您可以选择添加参数>扩展选择参数。然后单击“生成管道脚本”进行转换。 - ahmoreish
1
这正是我所需要的 +1 @icey - bhordupur

2

就像mkobit所说,目前无法将扩展选择插件用作构建参数。

我想使用的解决方法是以下结构:

timeout(time: 5, unit: TimeUnit.MINUTES) {
    def result = input(message: 'Set some values', parameters: [
        booleanParam(defaultValue: true, description: '', name: 'SomeBoolean'),
        choice(choices: "Choice One\nChoice Two", description: '', name: 'SomeChoice'),
        stringParam(defaultValue: "Text", description: '', name: 'SomeText')
    ]) as Map<String, String>
}

echo "${result.SomeBoolean}, ${result.SomeChoice}, ${result.SomeText}"

在我的流水线开始时调用它。然后,在构建启动后不久,您将被要求提供这些输入。


1
请使用以下代码构建多选复选框参数:
parameters {
  extendedChoice description: '', multiSelectDelimiter: ',', name: 'a', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'a,b,c', visibleItemCount: 3
}

它在Jenkins用户界面中看起来像这样:

enter image description here

使用声明性指令生成器生成各种使用Extended Choice Parameter插件的管道源代码。

enter image description here


1

我的工作是:

我需要从Nexus Repo检索所有工件的版本号:

 properties ([
    parameters([
        choice(choices: ['PROD', 'DEV', 'QA'], description: '', name: 'ParamEnv' ),   
        string(name: 'ParamVersion', defaultValue: '', description: 'Version to deploy'),
        extendedChoice(
            name: 'someName',
            description: '',
            visibleItemCount: 50,
            multiSelectDelimiter: ',',
            type: 'PT_SINGLE_SELECT',
            groovyScript: '''
            import groovy.json.JsonSlurper
                List<String> nexusPkgV = new ArrayList<String>()        
                def pkgObject = ["curl", "https://xxxx:xxxx@xxxxxxxxxx"].execute().text
                def jsonSlurper = new JsonSlurper()
                def artifactsJsonObject = jsonSlurper.parseText(pkgObject)
                def dataA = artifactsJsonObject.items
                for (i in dataA) {
                    nexusPkgV.add(i.version)
                }
            return nexusPkgV
            '''
        )
    ])
]) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接