3

I am trying to implement a Jenkins pipeline whereby I want to control the source code for the pipeline code in git.

To declare my parameters in a Declarative pipeline, I'll have to adhere to the following syntax:-

...
pipeline {
    parameters {
        ...
    }
...
}

For the parameters section, how can I declare an Active Choice Reactive parameter, so that I can programatically populate the choices using a groovy script?

I know this is possible using the Jenkins UI using the Configure pipeline option, however I am trying to understand how I can implement this behavior in Groovy code.

Thank you

cyberbeast
  • 678
  • 1
  • 12
  • 30

1 Answers1

5

Jenkins dynamic declarative pipeline parameters

Look at solution in post #5, not the accepted. It works well always. This is the only way for now cause Active Choices (AC) built for scripted pipelines and not straightforward support declarative pipelines. I work a lot with AC in declarative pipelines, so my own decision is always move all the properties (parameters) wrote in scripted way before "pipeline" and even use it in Shared Libraries (as init function) or load from disc if needed and all other pipeline writing in declarative way to get all the advantages of both.

Additional benefit of that trick that I can reuse pipeline with different parameters cause loading them on the fly from Git or Shared Libraries.

Found lifehack: To built Groovy script of different codes and AC types go to pipeline syntax constructor, select "input" > "This build is parametrized" > add there needed AC > fill all the fields and choose options as needed (same as in Job UI) > Generate code. After just copy "properties block" or part of AC code and put it in your Jenkinsfile before "pipeline".

Dmitry G.
  • 51
  • 1
  • 3