📖 Scenario: You are setting up a Jenkins pipeline for a simple project. You want to allow users to provide input parameters when they start the pipeline. This helps customize the build process based on user choices.
🎯 Goal: Build a Jenkins pipeline script that declares a parameters block with specific input parameters. This will let users enter values before the pipeline runs.
📋 What You'll Learn
Declare a parameters block in the Jenkins pipeline script
Add a string parameter named BRANCH_NAME with default value main
Add a boolean parameter named RUN_TESTS with default value true
Add a choice parameter named DEPLOY_ENV with choices dev, qa, prod
Print the values of all parameters in the pipeline
💡 Why This Matters
🌍 Real World
Jenkins pipelines often need user input to decide which branch to build, whether to run tests, or where to deploy. Parameters block lets users provide this input easily.
💼 Career
Knowing how to declare and use parameters in Jenkins pipelines is essential for DevOps engineers to create flexible and user-friendly CI/CD workflows.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline script
Create a Jenkins pipeline script with a pipeline block and an empty parameters block inside pipeline. Use agent any to run on any available agent.
Jenkins
Hint
The parameters block goes inside the pipeline block but before stages.
2
Add string and boolean parameters
Inside the parameters block, add a string parameter named BRANCH_NAME with default value main, and a boolean parameter named RUN_TESTS with default value true.
Jenkins
Hint
Use string(name: '', defaultValue: '', description: '') and booleanParam(name: '', defaultValue: , description: '') inside parameters.
3
Add a choice parameter for deployment environment
Add a choice parameter named DEPLOY_ENV inside the parameters block. It should have choices dev, qa, and prod.
Jenkins
Hint
Use choice(name: '', choices: ['option1', 'option2'], description: '') inside parameters.
4
Print all parameter values in the pipeline
Inside the steps block of the Example stage, add echo statements to print the values of BRANCH_NAME, RUN_TESTS, and DEPLOY_ENV parameters.
Jenkins
Hint
Use echo "BRANCH_NAME: ${params.BRANCH_NAME}" to print parameter values inside steps.
Practice
(1/5)
1. What is the purpose of the parameters block in a Jenkins pipeline?
easy
A. To define inputs that users can provide before the job runs
B. To specify the stages of the pipeline
C. To set environment variables for the pipeline
D. To declare the agent where the pipeline runs
Solution
Step 1: Understand the role of the parameters block
The parameters block is used to ask users for input before the pipeline starts.
Step 2: Compare with other pipeline blocks
Other blocks like stages define pipeline steps, environment sets variables, and agent defines the execution node.
Final Answer:
To define inputs that users can provide before the job runs -> Option A
Quick Check:
parameters block = user inputs [OK]
Hint: Parameters block = user input before job runs [OK]
Common Mistakes:
Confusing parameters with stages
Thinking parameters set environment variables
Mixing parameters with agent declaration
2. Which of the following is the correct syntax to declare a string parameter named VERSION in a Jenkins pipeline?
The correct syntax uses string(name: 'NAME', defaultValue: 'value', description: 'text').
Step 2: Check each option
parameters { string(name: 'VERSION', defaultValue: '1.0', description: 'Version number') } matches the correct syntax. The other options use invalid or incorrect syntax.
A. The choices string should use commas instead of newlines
B. The string parameter uses 'default' instead of 'defaultValue'
C. The choice parameter name must be uppercase
D. Parameters block cannot have multiple parameters
Solution
Step 1: Check choice parameter syntax
Choice parameter uses a newline-separated string for choices, which is correct.
Step 2: Check string parameter syntax
String parameter must use 'defaultValue', not 'default'. This causes a syntax error.
Final Answer:
The string parameter uses 'default' instead of 'defaultValue' -> Option B
Quick Check:
String param defaultValue key required [OK]
Hint: Use defaultValue, not default, for string parameters [OK]
Common Mistakes:
Using 'default' instead of 'defaultValue'
Misformatting choices string
Assuming parameter names must be uppercase
5. You want to create a Jenkins pipeline that asks the user to select multiple branches to build from a list, but Jenkins does not have a built-in multi-select parameter. How can you simulate this using the parameters block?
hard
A. Use a string parameter where the user enters branch names separated by commas
B. Use multiple boolean parameters, one for each branch
C. Use a choice parameter with multiple selections enabled
D. Use a password parameter to hide branch names
Solution
Step 1: Understand Jenkins parameter limitations
Jenkins does not support multi-select choice parameters natively.
Step 2: Find workaround using string parameter
Using a string parameter, users can input multiple branch names separated by commas, which the pipeline can parse.
Step 3: Evaluate other options
Multiple booleans are cumbersome for many branches; choice parameter does not support multi-select; password parameter hides input but is unrelated.
Final Answer:
Use a string parameter where the user enters branch names separated by commas -> Option A
Quick Check:
Simulate multi-select with string input [OK]
Hint: Use comma-separated string input for multi-select simulation [OK]
Common Mistakes:
Expecting multi-select choice parameter exists
Using many booleans for many options
Misusing password parameter for selection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.