1

UPDATE

I have a simple pipeline where I want to receive in parameters multiple choices from a file. In my file I have

@Test1,Accounts @Test2,Services @Test3,Accesses 

and I want to have all of "@Test1", "@Test2" and "@Test3" in checkboxes as parameters so I would run only the tests selected. But I'm not understanding what I'm doing wrong. Pipeline

def code = """tests = getChoices() return tests def getChoices() { def filecontent = readFile "/var/jenkins_home/test.txt" def stringList = [] for (line in filecontent.readLines()) {stringList.add(line.split(",")[0].toString())} List modifiedList = stringList.collect{'"' + it + '"'} return modifiedList }""".stripIndent() properties([ parameters([ [$class : 'CascadeChoiceParameter', choiceType : 'PT_CHECKBOX', description : 'Select a choice', filterLength : 1, filterable : false, name : 'choice1', referencedParameters: 'role', script : [$class : 'GroovyScript', fallbackScript: [ classpath: [], sandbox : true, script : 'return ["ERROR"]' ], script : [ classpath: [], sandbox : true, script : code ] ] ] ]) ]) pipeline { agent { docker { image 'node:latest' } } stages { stage('Tags') { steps { getChoices() } } } } def getChoices() { def filecontent = readFile "/var/jenkins_home/test.txt" def stringList = [] for (line in filecontent.readLines()) { stringList.add(line.split(',')[0].toString()) } List modifiedList = stringList.collect { '"' + it + '"' } echo "$modifiedList" return modifiedList } 

With this approach I know I can use multi-select checkboxes because I tried to substitute

def code = """ tests = ["Test1", "Test2", "Test3"] return tests""".stripIndent() 

and I get the output that I wanted.

But when I run my pipeline I get build SUCCESS but always get fallbackScript in my Build parameters checkbox. Can anyone help me out understand what is causing fallbackScript to run always? Thanks :)

1 Answer 1

1

If you want to auto-populate build parameters you have to return a list of parameters from your function. When you execute the pipeline the build with parameters will be populated. Note in this was only from the second execution of the pipeline the new parameters will be available. Refer following.

pipeline { agent any parameters{ choice(name: 'TESTES', choices: tests() , description: 'example') } stages { stage('Hello') { steps { echo 'Hello World' } } } } def tests() { return ["Test01", "Test2", "Test4"] } 

If you want to get user input each time you execute a build you should move your choice parameter into a stage. Please refer to the following.

pipeline { agent any stages { stage('Get Parameters') { steps { script{ def choice = input message: 'Please select', ok: 'Next', parameters: [choice(name: 'PRODUCT', choices: tests(), description: 'Please select the test')] echo '$choice' } } } } } def tests() { return ["Test01", "Test2", "Test4"] } 

Update 02

Following is how to read from a file and dynamically create the choice list.

pipeline { agent any stages { stage('Get Parameters') { steps { script{ sh''' echo "@Test1,Accounts" >> test.txt echo "@Test2,Services" >> test.txt ''' def choice = input message: 'Please select', ok: 'Next', parameters: [choice(name: 'PRODUCT', choices: getChoices(), description: 'Please select the test')] } } } } } def getChoices() { def filecontent = readFile "test.txt" def choices = [] for(line in filecontent.readLines()) { echo "$line" choices.add(line.split(',')[0].split('@')[1]) } return choices } 
Sign up to request clarification or add additional context in comments.

5 Comments

This example that you gave is only for static choices. But imagine that I Change my file, I would like to retrieve in choices all tests and don't need to pass them manually
Thank you so much . This is what I want but I want to select Multiple tests . Is that possible?
Yes, it's possible. You can use a plugin like the extended choice plugin or active choice plugin for this. plugins.jenkins.io/uno-choice
Updated my question, if you could see and help me out I would appreciate :) Thank you so much for your help btw !
@wsn Please don't update questions to ask different questions. I would suggest reverting back to your original question. If you need help with a different question feel free to create a new question. Also, if the answer is helpful consider accepting it as an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.