如何在 Jenkins 文件中传递多 select 值参数 (Groovy)
How to pass multi select value parameter in Jenkins file(Groovy)
例如下面的代码用于单个 select 值
choice{
choices: 'Box\nOneDrive\nSharePointOnline\nGmail\nGDrive\nGenericS3',
defaultValue: 'box',
description: 'Connector to build',
name: 'On_Cloud_Devices_To_Test'
}
我会使用布尔参数。然后用户可以勾选所有需要的选项。
booleanParam(defaultValue: false, name: 'ALL', description: 'Process all'),
booleanParam(defaultValue: false, name: 'OPTION_1', description: 'Process option 1'),
booleanParam(defaultValue: false, name: 'OPTION_2', description: 'Process options 2'),
您可以使用 "Extended Choice Parameter" 插件,正如 this page 所建议的那样。
因为参数列表很长,你可以把它包装成一个函数:
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition
def checkBox (String name, String values, String defaultValue,
int visibleItemCnt=0, String description='', String delimiter=',') {
// default same as number of values
visibleItemCnt = visibleItemCnt ?: values.split(',').size()
return new ExtendedChoiceParameterDefinition(
name, //name,
"PT_CHECKBOX", //type
values, //value
"", //projectName
"", //propertyFile
"", //groovyScript
"", //groovyScriptFile
"", //bindings
"", //groovyClasspath
"", //propertyKey
defaultValue, //defaultValue
"", //defaultPropertyFile
"", //defaultGroovyScript
"", //defaultGroovyScriptFile
"", //defaultBindings
"", //defaultGroovyClasspath
"", //defaultPropertyKey
"", //descriptionPropertyValue
"", //descriptionPropertyFile
"", //descriptionGroovyScript
"", //descriptionGroovyScriptFile
"", //descriptionBindings
"", //descriptionGroovyClasspath
"", //descriptionPropertyKey
"", //javascriptFile
"", //javascript
false, //saveJSONParameterToFile
false, //quoteValue
visibleItemCnt, //visibleItemCount
description, //description
delimiter //multiSelectDelimiter
)
}
然后使用如下:
def testParam = checkBox("opt", // name
"opt1,opt2,opt3", // values
"opt1", //default value
0, //visible item cnt
"Multi-select", // description
)
properties(
[parameters([testParam])]
)
node {
echo "${params.opt}"
}
Jenkins 使用参数构建:
顺便说一句,这是脚本化管道语法。
例如下面的代码用于单个 select 值
choice{
choices: 'Box\nOneDrive\nSharePointOnline\nGmail\nGDrive\nGenericS3',
defaultValue: 'box',
description: 'Connector to build',
name: 'On_Cloud_Devices_To_Test'
}
我会使用布尔参数。然后用户可以勾选所有需要的选项。
booleanParam(defaultValue: false, name: 'ALL', description: 'Process all'),
booleanParam(defaultValue: false, name: 'OPTION_1', description: 'Process option 1'),
booleanParam(defaultValue: false, name: 'OPTION_2', description: 'Process options 2'),
您可以使用 "Extended Choice Parameter" 插件,正如 this page 所建议的那样。
因为参数列表很长,你可以把它包装成一个函数:
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition
def checkBox (String name, String values, String defaultValue,
int visibleItemCnt=0, String description='', String delimiter=',') {
// default same as number of values
visibleItemCnt = visibleItemCnt ?: values.split(',').size()
return new ExtendedChoiceParameterDefinition(
name, //name,
"PT_CHECKBOX", //type
values, //value
"", //projectName
"", //propertyFile
"", //groovyScript
"", //groovyScriptFile
"", //bindings
"", //groovyClasspath
"", //propertyKey
defaultValue, //defaultValue
"", //defaultPropertyFile
"", //defaultGroovyScript
"", //defaultGroovyScriptFile
"", //defaultBindings
"", //defaultGroovyClasspath
"", //defaultPropertyKey
"", //descriptionPropertyValue
"", //descriptionPropertyFile
"", //descriptionGroovyScript
"", //descriptionGroovyScriptFile
"", //descriptionBindings
"", //descriptionGroovyClasspath
"", //descriptionPropertyKey
"", //javascriptFile
"", //javascript
false, //saveJSONParameterToFile
false, //quoteValue
visibleItemCnt, //visibleItemCount
description, //description
delimiter //multiSelectDelimiter
)
}
然后使用如下:
def testParam = checkBox("opt", // name
"opt1,opt2,opt3", // values
"opt1", //default value
0, //visible item cnt
"Multi-select", // description
)
properties(
[parameters([testParam])]
)
node {
echo "${params.opt}"
}
Jenkins 使用参数构建:
顺便说一句,这是脚本化管道语法。