【发布时间】:2017-11-07 00:21:57
【问题描述】:
我的 Jenkins 中安装了活动选择参数。在这里,我必须从本地存储的文本文件中选择一个值,然后将其作为多选选项显示给用户。 例如,我的文本文件可以有一个条目 1 - abc,2 - def。用户必须看到这两个条目,并且可以选择其中一个或两个。此文本文件是手动维护的。我可以有第三个条目 3 - ghi,当我触发 Jenkins 作业时,我应该会看到所有 3 个条目。
谁能帮帮我?
提前谢谢你, 罗希特
【问题讨论】:
我的 Jenkins 中安装了活动选择参数。在这里,我必须从本地存储的文本文件中选择一个值,然后将其作为多选选项显示给用户。 例如,我的文本文件可以有一个条目 1 - abc,2 - def。用户必须看到这两个条目,并且可以选择其中一个或两个。此文本文件是手动维护的。我可以有第三个条目 3 - ghi,当我触发 Jenkins 作业时,我应该会看到所有 3 个条目。
谁能帮帮我?
提前谢谢你, 罗希特
【问题讨论】:
试试这个:
// create blank array/list to hold choice options for this parameter and also define any other variables.
def list = []
def file = "/opt/data/jenkins/jobs/dummy_dummy/workspace/somefile.txt"
// create a file handle named as textfile
File textfile= new File(file)
// now read each line from the file (using the file handle we created above)
textfile.eachLine { line ->
//add the entry to a list variable which we'll return at the end.
//The following will take care of any values which will have
//multiple '-' characters in the VALUE part
list.add(line.split('-')[1..-1].join(',').replaceAll(',','-'))
}
//Just fyi - return will work here, print/println will not work inside active choice groovy script / scriptler script for giving mychoice parameter the available options.
return list
【讨论】:
您可以使用 groovy 脚本来解析文件并返回选项,例如:
def list = []
File textfile= new File("path-to-file")
textfile.eachline { line ->
//assuming you want only text values without a number
list.add(line.split('-')[1]) }
return list
同时为多个值选择“选择类型”。
【讨论】: