How to Use Parameters in Jenkins Pipeline for Dynamic Builds
In Jenkins Pipeline, you use the
parameters block to define input parameters that users can set before running the job. These parameters can be accessed inside the pipeline script using params.PARAMETER_NAME to customize the build behavior dynamically.Syntax
The parameters block is placed inside the pipeline block in a Jenkinsfile. You define each parameter type like string, booleanParam, or choice with a name and default value. Inside the pipeline stages, you access these parameters using params.PARAMETER_NAME.
Example parameter types:
string: Text inputbooleanParam: True/False checkboxchoice: Dropdown list
groovy
pipeline {
agent any
parameters {
string(name: 'GREETING', defaultValue: 'Hello', description: 'Greeting message')
booleanParam(name: 'SHOULD_RUN', defaultValue: true, description: 'Run the build?')
choice(name: 'ENV', choices: ['dev', 'test', 'prod'], description: 'Select environment')
}
stages {
stage('Example') {
steps {
script {
if (params.SHOULD_RUN) {
echo "${params.GREETING}, running on ${params.ENV} environment."
} else {
echo 'Build skipped.'
}
}
}
}
}
}Example
This example shows a Jenkins Pipeline with three parameters: a string, a boolean, and a choice. The pipeline prints a greeting message only if the boolean parameter is true, using the selected environment from the choice parameter.
groovy
pipeline {
agent any
parameters {
string(name: 'GREETING', defaultValue: 'Hi', description: 'Greeting message')
booleanParam(name: 'RUN_BUILD', defaultValue: true, description: 'Run the build?')
choice(name: 'ENVIRONMENT', choices: ['development', 'staging', 'production'], description: 'Choose environment')
}
stages {
stage('Greet') {
steps {
script {
if (params.RUN_BUILD) {
echo "${params.GREETING}, this build runs on ${params.ENVIRONMENT}."
} else {
echo 'Build was skipped by user.'
}
}
}
}
}
}Output
[Pipeline] echo
Hi, this build runs on staging.
[Pipeline] End of Pipeline
Common Pitfalls
Common mistakes when using parameters in Jenkins Pipeline include:
- Not defining the
parametersblock inside thepipelineblock, causing parameters to be ignored. - Using
params.PARAMETER_NAMEbefore the parameters are defined or outside the pipeline script. - Forgetting to use
scriptblock when accessing parameters in declarative pipelines. - Using wrong parameter names or case sensitivity issues.
groovy
/* Wrong: parameters block outside pipeline */ parameters { string(name: 'NAME', defaultValue: 'User') } pipeline { agent any stages { stage('Test') { steps { echo "Hello ${params.NAME}" } } } } /* Right: parameters block inside pipeline */ pipeline { agent any parameters { string(name: 'NAME', defaultValue: 'User') } stages { stage('Test') { steps { echo "Hello ${params.NAME}" } } } }
Quick Reference
Here is a quick summary of common parameter types and how to access them:
| Parameter Type | Definition Example | Access in Pipeline |
|---|---|---|
| String | string(name: 'NAME', defaultValue: 'User') | params.NAME |
| Boolean | booleanParam(name: 'FLAG', defaultValue: true) | params.FLAG |
| Choice | choice(name: 'ENV', choices: ['dev','prod']) | params.ENV |
Key Takeaways
Define parameters inside the pipeline block using the parameters section.
Access parameters in the pipeline script with params.PARAMETER_NAME.
Use script blocks when referencing parameters in declarative pipelines.
Common parameter types include string, booleanParam, and choice.
Always verify parameter names and placement to avoid errors.