title: Jenkins dynamic declarative pipeline parameters
body: <p>Can the parameters in a Jenkins declarative pipeline be dynamic?</p>
  
  <p>I want a the choice option values be populated at runtime by a function. The following code does generate  a list of options, but they seem to be stale - probably generated on the first time I ran this code. If the list of AMIs changes, the choices remain the same. I want this to run every time I select <code>build with parameters</code>.</p>
  
  <pre><code>def findAMIs() {
      // Find relevant AMIs based on their name
      def sout = new StringBuffer(), serr = new StringBuffer()
      def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \
                 ' --owners OWNER --filter Name=name,Values=PATTERN \
                 ' --query Images[*].{AMI:Name} --output  text'.execute()
      proc.consumeProcessOutput(sout, serr)
      proc.waitForOrKill(10000)
      return sout.tokenize() 
  }
  
  def AMIs = findAMIs().join('\n')
  
  pipeline {
      // a declarative pipeline
      agent any
  
      parameters {
          choice(name: 'Release',
                 choices: AMIs)
      }
      ...
   }
  </code></pre>
  
  <p><strong>EDIT</strong>
  I ended up using <code>jenkins-job-builder</code>, with extended choice parameters. It does not support the <code>groovyScript</code> parameter at the moment, so I modified it <a href="https://review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z" rel="noreferrer">https://review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z</a></p>
  
  <p><strong>EDIT</strong>
  The above link went dead, so here is another link to openstack:
  <a href="https://review.opendev.org/#/c/477003/" rel="noreferrer">https://review.opendev.org/#/c/477003/</a>
  But the gist of the matter is I have added a new parameter to jenkins-job-builder called 'groovyScriptFile', which was merged.</p>