Search code examples
jenkinsgroovyjenkins-pluginsjenkins-groovy

How to read all file names in a directory with jenkins active choices parameter?


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.


Solution

  • 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