0

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

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

For more, see reference post here: stackoverflow.com/questions/45336944/…
0

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.

6 Comments

THank you Igor. I have added the code provided by you. But when I choose Build with parameters, ideally I should see abc, def, ghi etc, but I do not see anything...
@RohitSavanurkar Did you check your script? You can run it in http://<your-jenkins-server/script> and see if its return those values from the file.
@RohitSavanurkar What I ment is, you should first check if the script is correct. Please run it in Jenkins script console and paste the output
Hello @Igor. Not sure how to run in Jenkins script. Can you please tell me how do I do it? I mean run the Jenkins script.
@RohitSavanurkar as I mentioned, go to your_jenkins_url/script, paste there your script and see what is the output
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.