jenkins - 如何在声明性 Jenkins 文件内的 Jenkins 多管道中设置响应式(Reactive)选择参数

标签 jenkins groovy parameters

当我为扩展选择参数选择值时,我需要一个响应式(Reactive)选择参数来为我提供 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 的相同内容。

enter image description here

关于jenkins - 如何在声明性 Jenkins 文件内的 Jenkins 多管道中设置响应式(Reactive)选择参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68010520/

相关文章:

javascript - Selenium IDE/Builder 可以在多个页面上运行相同的测试用例吗?

jenkins - 为什么 Jenkins 2.124 上找不到 s3Upload 构建步骤

android - 从 Pipeline 项目发布 Android Lint 结果

java - 如何在adf中使用groovy表达式显示Max

web-services - 使用 CXF(实际上是 GroovyWS),我如何生成一个带有一个带有文本节点的子节点的 SOAP header ?

c - 两个带有 void 和空参数列表的函数声明

JAVA随机参数总是返回0

从 Artifactory 删除大文件夹时,Jenkins 出现意外异常 java.nio.channels.ClosedByInterruptException

python : call a function with n arguments

Groovy null 小于零?