title: Populate choice parameter in jenkinsfile with list of folders in SCM repository
body: <p>I've got a Jenkinsfile that drives a pipeline which the user must select a specific folder in a bitbucket repo to target. I want that choice parameter dropdown to be dynamically populated.</p>
  <p>Currently, I've got the choice param list hardcoded as per this generic example:</p>
  <pre><code>choice(name: 'IMAGE', choices: ['workload-x','workload-y','workload-z'])
  </code></pre>
  <p>I wondered if this is possible from within the jenkinsfile itself, or whether I'd have to create a specific groovy script for this then call it. Either way I'm a bit lost as I'm pretty new to jenkins and have only just started working with Jenkinsfiles.</p>
  <p>Some trial and error googling has allowed me to create a groovy script which returns an array of folder names in the repository using json slurper:</p>
  <pre><code>import groovy.json.JsonSlurper
  import jenkins.model.Jenkins
  
  def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
          com.cloudbees.plugins.credentials.Credentials.class,
          Jenkins.instance,
          null,
          null
      );
  
  def credential = creds.find {it.id == &quot;MYBITBUCKETCRED&quot;}
  
  if (!credential) { return &quot;Unable to pickup credential from Jenkins&quot; }
  username = credential.username
  pass = credential.password.toString()
  
  def urlStr = &quot;https://bitbucket.mydomain.com/rest/api/1.0/projects/MYPROJECT/repos/MYREPO/browse/&quot;
  
  HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection()
  String encoded = Base64.getEncoder().encodeToString((username + &quot;:&quot; + pass).getBytes(&quot;UTF-8&quot;));
  conn.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + encoded);
  conn.connect();
  
  def slurper = new JsonSlurper() 
  def browseList = slurper.parseText(conn.getInputStream().getText())
  
  def dfList = browseList.children.values.path.name.findAll {it.contains('workload-')}
  
  return dfList
  </code></pre>
  <p>This returns a result as follows:</p>
  <pre><code>Result:   [workload-a,workload-b,workload-c,workload-x,workload-y,workload-z]
  </code></pre>
  <p>However I'm unsure how then to call this in my Jenkinsfile in order to populate the dropdown.</p>
  <p>Any help would be greatly appreciated.</p>