如何在声明性 Jenkins 文件中的 Jenkins 多管道中设置反应选择参数

时间:2021-06-16 22:00:38

标签: jenkins groovy parameters

当我为扩展选择参数选择一个值时,我需要一个反应式选择参数来为我提供 1-3 个选项。

以下代码给了我错误:

<块引用>

org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败:WorkflowScript:4:无法定义构建参数 作为地图@第4行,第9列。 [$class: 'CascadeChoiceParameter', ^

1 个错误

pipeline {
    parameters {
        extendedChoice(defaultValue: 'none', description: 'Select the resource you want to modify', multiSelectDelimiter: ',', name: 'Resources', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', value: 'Compute, LIVE', visibleItemCount: 2) 
        [$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_SINGLE_SELECT',
        description: 'Select a choice',
        filterLength: 1,
        filterable: true,
        name: 'choice1',
        referencedParameters: 'Resources',
        script: [$class: 'GroovyScript',
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 'return ["ERROR"]'
            ],
            script: [
                classpath: [], 
                sandbox: true, 
                script: """
                    if (Resources == 'Compute') { 
                        return['aaa','bbb']
                    }
                    else {
                        return['ccc', 'ddd']
                    }
                """.stripIndent()
            ]
        ]
    ]

如何在声明式管道中实现这一点?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以根据您的要求使用以下代码。

 properties([
    parameters([
        
        extendedChoice(defaultValue: 'none', description: 'Select the resource you want to modify', multiSelectDelimiter: ',', name: 'Resources', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', value: 'Compute, LIVE', visibleItemCount: 2),
        [$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_SINGLE_SELECT',
        description: 'Select a choice',
        name: 'choice1',
        referencedParameters: 'Resources',
        script: [$class: 'GroovyScript',
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 'return ["ERROR"]'
            ],
            script: [classpath: [], sandbox: false, script: '''
            def getChoice1(){
                def value = ""
                    switch(Resources) {
                        case "Compute":
                            value = ["aaa", "bbb"]
                            break
                        case "LIVE":
                            value = ["ccc", "ddd"]
                            break
                    }
            }
            def choice1 = getChoice1()
            return choice1
            '''
            ]
        ]
    ]
    ])
])

pipeline {
agent any
// do anything inside pipeline
}

下面是相同的 Jenkins UI。

enter image description here

答案 1 :(得分:1)

您可以使用 snippet generator tool 生成管道脚本。
在进入主要代码之前,我会详细解释:
请参阅以下步骤:

  1. 选择Properties:Set job properties
    enter image description here
  2. 选择 project as paramterised 和 groovy 脚本 return ["Compute", "LIVE:selected"]

enter image description here 3. 添加 description 并选择 choice type
enter image description here
4. 再增加一个Active choice反应参数和groovy脚本
enter image description here 5.选择选择类型并确保添加您将使用的参考参数的名称,如下所示:
enter image description here
6.点击生成管道脚本:
enter image description here

现在将其添加到管道作业中:以下是代码

properties([parameters([[$class: 'CascadeChoiceParameter', choiceType: 'PT_SINGLE_SELECT', description: 'Select the resource you want to modify', filterLength: 1, filterable: false, name: 'Resources', randomName: 'choice-parameter-843794284471400', referencedParameters: '',
script: [$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: false, script: ''],
script: [classpath: [], sandbox: false,
script: 'return ["Compute", "LIVE:selected"]']]],
[$class: 'CascadeChoiceParameter', 
 choiceType: 'PT_SINGLE_SELECT',
 description: '', 
 filterLength: 1, filterable: false,
 name: 'choice1', 
 randomName: 'choice-parameter-843794300545400',
 referencedParameters: 'Resources',
 script: [$class: 'GroovyScript',
 fallbackScript: [classpath: [], sandbox: false, script: ''],
 script: [classpath: [], sandbox: false, 
 script: '''
 if (Resources.equals("Compute")){
                                return["aaaa","bbbb"]
                                  }
 else if(Resources.equals("LIVE")){
                                return["cccc","dddd"]
                            }''']]]])])

输出
enter image description here enter image description here

相关问题