13

How can I use the Jenkins Dynamic Plugin in a Jenkinsfile?

What I am looking for is a Jenkinsfile snippet that:

  • Enables the Build with Parameters option in the Jenkins job
  • When selected, a script that populates a list that can be used Dynamic Choice Parameters is populated and the user will see a drop down list.

When trying:

  1. Pipeline syntax in the Jenkins editor
  2. Selecting properties: Set job properties as Sample step
  3. Selecting This project is parameterized
  4. Using Dynamic Choice Parameter
  5. Enter values for Name, Choice Script, Remote Script etc
  6. Generate Pipeline Script

I get the following template:

properties([
    parameters([
        <object of type com.seitenbau.jenkins.plugins.dynamicparameter.ChoiceParameterDefinition>
    ]), 
    pipelineTriggers([])
])

i.e. the generated pipeline script does not contain the data that I have entered in step 5. above. How can I modify parameters so that parameter name, choices, etc will be visible to the user?


Jenkins version: 2.19.3 Dynamic Parameter Plugin version: 0.2.0

StephenKing
  • 36,187
  • 11
  • 83
  • 112
matsev
  • 32,104
  • 16
  • 121
  • 156

1 Answers1

11

there is no need anymore for the Jenkins Dynamic Plugin anymore. Just use the normal choice or string parameter and have the value(s) updated by groovy code.

#!/bin/groovy

def envs = loadEnvs();

properties([
   parameters([
      choice(choices: envs, description: 'Please select an environment', name: 'Env')
   ])
])

node { 
   try {
      stage('Preparation'){
...

If you use the choice parameter be aware the you must provide a string where the values are separated by a new line.

For example:

"a\nb\nc"

If you really need to plugin, then vote on this issue JENKINS-42149.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Karel Striegel
  • 1,319
  • 1
  • 9
  • 8
  • 1
    In what version of Jenkins? I'm running 2.3 and get the error "No such DSL method 'parameters' found among steps" – Aaron McMillin Mar 08 '17 at 04:42
  • It works, but it creates new parameter and persists values in build parameters, if actual values (for example list of maven versions) changed -parameters values not updated, seems loadEnvs() not executed every time or smth like this – Georgy Gobozov Nov 02 '17 at 19:06
  • 1
    This is a scripted pipeline. Does it work in declarative pipeline? If so, how? What's the syntax? – Andrew Gray Mar 22 '18 at 10:06
  • 16
    The problem with this is changes to build properties only affect _future_ builds -- changing the parameters on the current build doesn't help because the `properties()` function runs after the user has already provided the build parameters for that build – Michael Mrozek Apr 10 '18 at 06:27