0
0
Jenkinsdevops~5 mins

Dynamic parameter values in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your Jenkins job to ask for input that changes depending on other choices or external data. Dynamic parameter values let your job show options that update automatically, making your builds smarter and easier to use.
When you want to let users pick a server from a list that updates every time the job runs.
When you need to select a branch from a Git repository and want the list to show current branches.
When you want to show different options based on a previous parameter choice in the same job.
When you want to fetch values from an external API or script to use as parameters.
When you want to reduce errors by limiting user input to valid, current options.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  parameters {
    choice(name: 'ENVIRONMENT', choices: ['dev', 'staging', 'production'], description: 'Select environment')
    activeChoiceReactiveParam(name: 'SERVER', description: 'Select server based on environment') {
      groovyScript {
        script("""
          if (ENVIRONMENT == 'dev') {
            return ['dev-server1', 'dev-server2']
          } else if (ENVIRONMENT == 'staging') {
            return ['staging-server1', 'staging-server2']
          } else {
            return ['prod-server1', 'prod-server2']
          }
        """)
        fallbackScript("return ['error']")
      }
      referencedParameters(['ENVIRONMENT'])
    }
  }
  stages {
    stage('Show Parameters') {
      steps {
        echo "Environment: ${params.ENVIRONMENT}"
        echo "Server: ${params.SERVER}"
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two parameters.

The first is a fixed choice parameter named ENVIRONMENT with three options.

The second is a dynamic choice parameter named SERVER whose options depend on the selected environment.

The activeChoiceReactiveParam block runs a Groovy script that returns a list of servers based on the ENVIRONMENT parameter.

During the build, Jenkins shows the server options that match the chosen environment.

Commands
This command updates or creates the Jenkins job with the dynamic parameters defined in the job configuration file.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline-job.yaml
Expected OutputExpected
Job 'my-pipeline-job' updated successfully\n
This command triggers the Jenkins job build, setting the ENVIRONMENT parameter to 'dev'. The SERVER parameter options will be dynamically set based on this.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 build my-pipeline-job -p ENVIRONMENT=dev
Expected OutputExpected
Started build #1\nWaiting for build to complete...\nFinished: SUCCESS\n
-p ENVIRONMENT=dev - Sets the ENVIRONMENT parameter to 'dev' for this build
This command fetches the console output of build number 1 to verify the parameters used during the build.
Terminal
java -jar jenkins-cli.jar -s http://localhost:8080 console my-pipeline-job 1
Expected OutputExpected
[Pipeline] echo\nEnvironment: dev\n[Pipeline] echo\nServer: dev-server1\n[Pipeline] End of Pipeline\n
Key Concept

Dynamic parameter values let Jenkins jobs show updated, context-aware options to users, improving input accuracy and flexibility.

Common Mistakes
Defining dynamic parameters without referencing other parameters or external data.
The parameter will not change dynamically and defeats the purpose of dynamic values.
Use scripts or functions that read other parameters or external sources to generate the dynamic list.
Not installing or enabling the required Jenkins plugins for dynamic parameters.
The dynamic parameter feature won't work and may cause errors or missing options.
Install plugins like 'Active Choices Plugin' and configure them properly before using dynamic parameters.
Returning an empty or invalid list from the dynamic parameter script.
The parameter will show no options or cause build failures.
Ensure the script always returns a valid non-empty list of strings for the parameter choices.
Summary
Define dynamic parameters in Jenkinsfiles or job configs to generate options based on other inputs or data.
Use scripts inside the parameter definition to return the list of valid choices dynamically.
Trigger builds with parameters and verify the dynamic values appear and work as expected.