Get all folders in a given directory with Groovy script (using Extended choice parameter in Jenkins)

3.9k Views Asked by At

Im using Extended Choice Parameter in Jenkins. I want to add a drop down parameter in the job and display all folder names within the given directory using a groovy script

how can i do that ?

1

There are 1 best solutions below

2
On

You can use the following groovy script in the extended choice parameter to list all folders under a given folder (You will probably require an admin approval to run this script):

import groovy.io.FileType

def list = []

def dir = new File("/var/lib/jenkins/workspace")
dir.eachFileRecurse (FileType.DIRECTORIES) { file ->
   list << file
}
return list

However, an easier option will be to use the Filesystem List Parameter Plugin.
The plugin lists file, directory or symlink names of a defined directory and exposes them as selection options for the parameter.
It also supports include/exclude patterns and execution on different nodes .