Reputation: 95
How can I read all file names in a directory and display them as a dropdown in Jenkins GUI?
My goal is actually creating two depending dropdowns where a user chooses a file in the first dropdown and regarding the choice, in the second dropdown, the first selected file content will be shown to the user as a parameter.
I've tried the following but without success.
In the Active Choice Parameter - Groovy Script:
import groovy.io.FileType
def list = []
def dir = new File("/some/path/to/variable/TEST/*")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
return list
Here Active Choice Parameter Name is SWITCH
and
In the Active Choices Reactive Parameter
import groovy.json.JsonSlurper
def list = []
File textfile= new File("/some/path/to/variable/${SWITCH}")
JsonSlurper slurper = new JsonSlurper()
def parsedJson = slurper.parse(textfile)
parsedJson.each{ list.add(it.port_name + " " + it.description)}
return list
PS: Files are JSON formatted.
Upvotes: 1
Views: 3922
Reputation: 95
I've solved the problem so if anyone needs:
import groovy.io.FileType
def list = []
def dir = new File("/some/path/to/variable/TEST/")
dir.eachFileRecurse (FileType.FILES) { file ->
list.add(file.getName())
}
return list
Upvotes: 1
Reputation: 484
I think you miss the option Referenced parameters
as in the screenshot:
The description of this filed is here:
This option, takes a list of job parameters that trigger an auto-refresh of the Reactive Parameter when any of the 'Referenced parameters' change
Update:
For me works with your code, except one small change:
import groovy.json.JsonSlurper
def list = []
File textfile= new File("${SWITCH}")
JsonSlurper slurper = new JsonSlurper()
def parsedJson = slurper.parse(textfile)
parsedJson.each {
list.add "$it.port_name $it.description".toString()
}
return list
I added toString()
for each list element to convert the items of GString type to String. I assume this was your problem. Without toString()
, Jenkins displays [Object object] because it does not know how to convert it.
Upvotes: 0