Create drop down list selection of files populated from git repository in Pipeline

Issue

  • I would like to create a drop-down list selection of files populated from a GIT repository.

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline

Resolution

The following Pipeline example populates a drop down list selection step using property files (by name) found in a git repository. The selected value is passed to a parameterized downstream job as a String parameter.

node {
    // Change `url` value to your own
    git url: 'file:////Users/evildethow/workspace/spike/project-w-property-files.git'
    
    def inputParams = inputParamsString(new File(pwd()))
    
    // Change `message` value to the message you want to display
    // Change `description` value to the description you want
    def selectedProperty = input( id: 'userInput', message: 'Choose properties file', parameters: [ [$class: 'ChoiceParameterDefinition', choices: inputParams, description: 'Properties', name: 'prop'] ])
    
    println "Property: $selectedProperty"
    
    // Change `job` value to your downstream job name
    // Change `name` value to the name you gave the string parameter in your downstream job
    build job: 'downstream-freestyle', parameters: [[$class: 'StringParameterValue', name: 'prop', value: selectedProperty]]
}

import static groovy.io.FileType.FILES

@NonCPS
def inputParamsString(dir) {
    def list = []
    
    // If you dont want to search recursively then change `eachFileRecurse` -> `eachFile`
    dir.eachFileRecurse(FILES) {
        // Change `.properties` to the file extenstion you are interested in
        if(it.name.endsWith('.properties')) {
            // If the full path is required remove `.getName()`
            list << it.getName()
        }
    }
    
    list.join("\n")
}

A reference of Pipeline steps used here:

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.