Reputation: 527
I have a Jenkins job that I need to set a parameter, if the user choose "EXISTING" to use an existing file, then the next parameter should be a dropdown with a list the existing file names for user to choose. If the user choose "NEW", the next parameter should be a string parameter for user to enter a file name.
I can not find how to use Active Choices Parameter and Active Choices Reactive Parameter to accomplish this. Maybe I am looking at the wrong direction?
For the first parameter CHOICE, I have the groovy script:
return [
"EXISTING",
"NEW"
]
for the second parameter FILES, I have this:
if (CHOICE.equals("EXISTING")) {
return ["file1","file2",..."filen"]
}
else if (CHOICE.equals("NEW")) {
return []
}
This does not do what I want.
How do I make the second parameter to allow user's input when the first parameter is "NEW"? Or to make the code to use two additional different parameters, one to show the existing file list when user choose EXISTING
and one to let user enter a file name if the user choose NEW
?
After I pick up the parameter values from above, I will pass them to a ansible playbook in the Build section.
Thanks!
Upvotes: 1
Views: 1067
Reputation: 14574
The following will work for you. If you want to get the choices from a function, you can also do that.
def INPUT_1 = ""
def INPUT_2 = ""
pipeline {
agent any
stages {
stage("Get File Name") {
steps{
timeout(time: 300, unit: 'SECONDS') {
script {
// The Initial Choice
INPUT_1 = input message: 'Please select', ok: 'Next',
parameters: [
choice(name: 'PRODUCT', choices: ['NEW','EXISTING'], description: 'Please select')]
if(INPUT_1.equals("NEW")) {
INPUT_2 = input message: 'Please Name the file', ok: 'Next',
parameters: [string(name: 'File Name', defaultValue: 'abcd.txt', description: 'Give me a name for the file?')]
} else {
INPUT_2 = input message: 'Please Select the File', ok: 'Next',
parameters: [
choice(name: 'File Name', choices: ["file1","file2","filen"], description: 'Select the file')]
}
echo "INPUT 1 ::: ${INPUT_1}"
echo "INPUT 2 ::: ${INPUT_2}"
}
}
}
}
}
post {
success {
echo 'The process is successfully Completed....'
}
}
}
Upvotes: 0