Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovy

How can I return file values in Jenkins choice parameter dropdown list?


I have this output.txt file:

cg-123456
cg-456789
cg-987654
cg-087431

Is it possible to get these values into a jenkins dropdown, like by using choice-parameter or active-choice-reactive parameter?


Solution

  • You can do something like the below.

    pipeline {
      agent any
      stages {
          stage ("Get Inputs") {
            steps {
             script{
              script {
                def input = input message: 'Please select the choice', ok: 'Ok',
                                            parameters: [
                                            choice(name: 'CHOICE', choices: getChoices(), description: 'Please select')]
              }
              
            }
            }
          }
      }
    }
    
    def getChoices() {
        filePath = "/path/to/file/output.txt"
    
        def choices = []
        def content = readFile(file: filePath)
        for(def line : content.split('\n')) {
            if(!line.allWhitespace){
                choices.add(line.trim())
            }
        }
        return choices
    }