Jenkins Pipeline with String, Boolean, and Choice Parameters
📖 Scenario: You are setting up a Jenkins pipeline job that needs user input before running. The inputs include a text string, a yes/no choice, and a selection from a list. This helps customize the build process.
🎯 Goal: Build a Jenkins pipeline script that defines three parameters: a string parameter called ENVIRONMENT, a boolean parameter called RUN_TESTS, and a choice parameter called DEPLOY_REGION. Then, use these parameters in the pipeline to print their values.
📋 What You'll Learn
Create a string parameter named ENVIRONMENT with default value development
Create a boolean parameter named RUN_TESTS with default value true
Create a choice parameter named DEPLOY_REGION with options us-east, us-west, and eu-central
Print the values of all three parameters in the pipeline
💡 Why This Matters
🌍 Real World
Jenkins pipelines often require user inputs to customize builds, such as choosing environments, toggling tests, or selecting deployment regions.
💼 Career
Knowing how to use parameters in Jenkins pipelines is essential for DevOps engineers to create flexible and interactive CI/CD workflows.
Progress0 / 4 steps
1
Define the ENVIRONMENT string parameter
Create a Jenkins pipeline script that defines a string parameter called ENVIRONMENT with the default value development inside the parameters block.
Jenkins
Hint
Use string(name: 'ENVIRONMENT', defaultValue: 'development', description: 'Target environment') inside the parameters block.
2
Add the RUN_TESTS boolean parameter
Add a boolean parameter called RUN_TESTS with the default value true inside the existing parameters block.
Jenkins
Hint
Use booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run tests before deploy') inside the parameters block.
3
Add the DEPLOY_REGION choice parameter
Add a choice parameter called DEPLOY_REGION with options us-east, us-west, and eu-central inside the parameters block.
Jenkins
Hint
Use choice(name: 'DEPLOY_REGION', choices: ['us-east', 'us-west', 'eu-central'], description: 'Deployment region') inside the parameters block.
4
Print all parameter values in the pipeline
Inside the steps block of the Example stage, add echo statements to print the values of ENVIRONMENT, RUN_TESTS, and DEPLOY_REGION parameters.
Jenkins
Hint
Use echo "ENVIRONMENT: ${params.ENVIRONMENT}", echo "RUN_TESTS: ${params.RUN_TESTS}", and echo "DEPLOY_REGION: ${params.DEPLOY_REGION}" inside the steps block.
Practice
(1/5)
1. What type of Jenkins parameter should you use if you want the user to enter any text value before running a job?
easy
A. String parameter
B. Boolean parameter
C. Choice parameter
D. File parameter
Solution
Step 1: Understand parameter types
String parameters allow users to input any text value.
Step 2: Match requirement to parameter
Since the question asks for text input, String parameter fits best.
Final Answer:
String parameter -> Option A
Quick Check:
Text input = String parameter [OK]
Hint: Text input? Always pick String parameter [OK]
Common Mistakes:
Choosing Boolean parameter for text input
Confusing Choice parameter with String parameter
Selecting File parameter which is for files
2. Which of the following is the correct syntax to define a Boolean parameter in a Jenkins pipeline script?
easy
A. choice(name: 'DEPLOY', choices: ['true', 'false'], description: 'Deploy to production?')
B. stringParam(name: 'DEPLOY', defaultValue: 'true', description: 'Deploy to production?')
C. booleanParam(name: 'DEPLOY', defaultValue: true, description: 'Deploy to production?')
D. boolParam(name: 'DEPLOY', default: true, desc: 'Deploy to production?')
Solution
Step 1: Recall Jenkins Boolean parameter syntax
The correct syntax uses booleanParam with name, defaultValue, and description keys.
Step 2: Compare options
booleanParam(name: 'DEPLOY', defaultValue: true, description: 'Deploy to production?') matches the correct syntax exactly; others have wrong names or keys.
Final Answer:
booleanParam(name: 'DEPLOY', defaultValue: true, description: 'Deploy to production?') -> Option C
When running the job, Jenkins throws an error. What is the problem?
medium
A. defaultValue should be a boolean, not a string
B. Parameter name cannot be uppercase
C. booleanParam does not accept description
D. Missing choice parameter for boolean
Solution
Step 1: Check booleanParam defaultValue type
defaultValue must be true or false (boolean), not 'false' (string).
Step 2: Identify error cause
Passing string 'false' causes type error in Jenkins pipeline.
Final Answer:
defaultValue should be a boolean, not a string -> Option A
Quick Check:
Boolean defaultValue = true/false, not 'true'/'false' [OK]
Hint: Boolean defaultValue must be true or false, no quotes [OK]
Common Mistakes:
Using string 'false' instead of boolean false
Thinking parameter names must be lowercase
Assuming booleanParam needs choice parameter
5. You want to create a Jenkins job that asks the user to select a deployment region from ['us-east', 'us-west', 'eu-central'], enter a version string, and confirm with a yes/no checkbox before deploying. Which parameter types should you use together?
hard
A. Choice parameter for region, Choice parameter for version, Boolean parameter for confirmation
B. String parameter for region, Boolean parameter for version, Choice parameter for confirmation
C. Boolean parameter for region, Choice parameter for version, String parameter for confirmation
D. Choice parameter for region, String parameter for version, Boolean parameter for confirmation
Solution
Step 1: Match each input to parameter type
Region selection from fixed list -> Choice parameter; version text -> String parameter; yes/no confirm -> Boolean parameter.
Step 2: Verify option matches all three correctly
Choice parameter for region, String parameter for version, Boolean parameter for confirmation correctly assigns Choice, String, and Boolean parameters respectively.
Final Answer:
Choice parameter for region, String parameter for version, Boolean parameter for confirmation -> Option D
Quick Check:
Dropdown = Choice, Text = String, Yes/No = Boolean [OK]