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

How to set up reactive choice parameter in Jenkins multipipeline inside declarative Jenkins file

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

以下代码给我的错误是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 4: Build parameters cannot be defined as maps @ line 4, column 9. [$class: 'CascadeChoiceParameter', ^

1 error

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()
            ]
        ]
    ]

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

谢谢

您可以使用下面的代码来满足您的要求。

 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 同样的。

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

  1. Select Properties:Set job properties:
  2. Select project as paramterised 连同 groovy 脚本 return ["Compute", "LIVE:selected"]

3.添加description和selectchoice type

4. 增加一个Active choice reactive parameter和groovy script
5.Select 选择类型并确保添加您将使用的引用参数的名称,如下所示:

6. 点击生成管道脚本:

现在在管道作业中添加:下面是代码

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"]
                            }''']]]])])

输出: