How to Use Parameterized Build in Jenkins: Simple Guide
In Jenkins, you enable a
parameterized build by checking the 'This project is parameterized' option in the job configuration and adding parameters like String or Boolean. When you run the build, Jenkins will ask for these inputs, which you can then use in your build steps or scripts.Syntax
To create a parameterized build in Jenkins, you configure the job with parameters. Each parameter has a type and a name. Common types include String Parameter, Boolean Parameter, and Choice Parameter.
Example syntax for a String parameter in a Jenkins Pipeline script:
groovy
properties([
parameters([
string(name: 'GREETING', defaultValue: 'Hello', description: 'Greeting message'),
booleanParam(name: 'SHOULD_PRINT', defaultValue: true, description: 'Print the greeting?')
])
])Example
This example shows a Jenkins Pipeline that uses two parameters: a string and a boolean. The pipeline prints the greeting only if the boolean is true.
groovy
pipeline {
agent any
parameters {
string(name: 'GREETING', defaultValue: 'Hello, Jenkins!', description: 'Greeting message')
booleanParam(name: 'SHOULD_PRINT', defaultValue: true, description: 'Print the greeting?')
}
stages {
stage('Print Greeting') {
steps {
script {
if (params.SHOULD_PRINT) {
echo params.GREETING
} else {
echo 'Skipping greeting print.'
}
}
}
}
}
}Output
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/example
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Print Greeting)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello, Jenkins!
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Common Pitfalls
- Not enabling 'This project is parameterized' in freestyle jobs causes parameters to be ignored.
- Using parameter names incorrectly in scripts (case-sensitive).
- For scripted pipelines, forgetting to define
propertieswith parameters causes no prompt. - Passing parameters to downstream jobs requires explicit forwarding.
groovy
/* Wrong: No properties block, parameters ignored */ node { echo params.MY_PARAM // This will fail if MY_PARAM is not defined } /* Right: Define properties with parameters */ properties([ parameters([ string(name: 'MY_PARAM', defaultValue: 'default', description: '') ]) ]) node { echo params.MY_PARAM }
Quick Reference
| Parameter Type | Description | Usage Example |
|---|---|---|
| String Parameter | User inputs a text string | string(name: 'NAME', defaultValue: 'User', description: 'Enter your name') |
| Boolean Parameter | User selects true or false | booleanParam(name: 'FLAG', defaultValue: false, description: 'Enable feature?') |
| Choice Parameter | User selects from a list | choice(name: 'ENV', choices: ['dev', 'test', 'prod'], description: 'Select environment') |
Key Takeaways
Enable 'This project is parameterized' to use build parameters in Jenkins jobs.
Define parameters clearly with names and types to prompt user input at build time.
Use parameters in your pipeline scripts via the 'params' object.
Always test parameter names for case sensitivity and correct usage.
Forward parameters explicitly when triggering downstream jobs.