Reputation: 1
I have active choices parameter installed in my Jenkins. Here I have to choose a value from the text file stored locally and then display this as a multi select option to the user. For example, my text file can have an entry 1 - abc, 2 - def. The user has to see both the entries and can select either of them or both of them. This text file is maintained manually. I can have a third entry 3 - ghi and when I trigger the Jenkins job, I should see all the 3 entries.
Can anyone please help me out here?
Thank you in advance, Rohit
Upvotes: 0
Views: 6066
Reputation: 17366
Try this:
// 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
Upvotes: 1
Reputation: 1208
You can use a groovy script to parse file and return options, something like:
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
Also choose "choice type" for multiple values.
Upvotes: 0