当我为扩展选择参数选择值时,我需要一个响应式(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 的相同内容。
关于jenkins - 如何在声明性 Jenkins 文件内的 Jenkins 多管道中设置响应式(Reactive)选择参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68010520/