📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process. You need to write Groovy code inside the Jenkinsfile to define variables, configure settings, and run steps.
🎯 Goal: Build a Jenkins pipeline script using Groovy syntax that defines a map of build parameters, sets a configuration variable, loops through the parameters to print them, and finally outputs a summary message.
📋 What You'll Learn
Create a Groovy map called buildParams with exact key-value pairs
Add a configuration variable called envName with a specific value
Use a for loop with variables param and value to iterate over buildParams
Print each parameter and its value inside the loop
Print a final summary message using println
💡 Why This Matters
🌍 Real World
Jenkins pipelines use Groovy syntax to automate build, test, and deployment tasks in software projects.
💼 Career
Understanding Groovy basics in Jenkinsfiles helps DevOps engineers write and maintain CI/CD pipelines efficiently.
Progress0 / 4 steps
1
Create the build parameters map
Create a Groovy map called buildParams with these exact entries: branch: 'main', buildTool: 'Maven', timeout: 30
Jenkins
Hint
Use Groovy map syntax with square brackets and colon-separated key-value pairs.
2
Add environment name configuration
Add a variable called envName and set it to the string 'production'
Jenkins
Hint
Define a simple string variable using def.
3
Loop through build parameters and print them
Use a for loop with variables param and value to iterate over buildParams. Inside the loop, print each parameter and its value using println and string interpolation.
Jenkins
Hint
Use keySet() to get keys and access values inside the loop.
4
Print the final summary message
Print a final message using println that says exactly: "Build configured for environment: production"
Jenkins
Hint
Use println with string interpolation to include envName.
Practice
(1/5)
1. In a Jenkins pipeline script using Groovy, what does the stage block represent?
easy
A. A loop for repeating tasks
B. A logical phase or step in the pipeline process
C. A function definition
D. A variable declaration
Solution
Step 1: Understand the purpose of stage in Jenkins pipelines
The stage block is used to define a distinct phase or step in the pipeline, such as build, test, or deploy.
Step 2: Differentiate from other Groovy constructs
It is not a variable, function, or loop but a way to organize pipeline tasks clearly.
Final Answer:
A logical phase or step in the pipeline process -> Option B
Quick Check:
stage = logical step [OK]
Hint: Remember: stage means a step or phase in pipeline [OK]
Common Mistakes:
Confusing stage with variable or function
Thinking stage is a loop
Assuming stage is a Groovy keyword unrelated to Jenkins
2. Which of the following is the correct syntax to define a variable name with value Jenkins in a Groovy pipeline script?
easy
A. name := "Jenkins"
B. var name = 'Jenkins'
C. def name = "Jenkins"
D. let name = 'Jenkins'
Solution
Step 1: Recall Groovy variable declaration syntax
Groovy uses def to declare variables, so def name = "Jenkins" is correct.
Step 2: Identify incorrect syntax
var and let are JavaScript keywords, and := is not Groovy syntax.
Final Answer:
def name = "Jenkins" -> Option C
Quick Check:
def declares variables in Groovy [OK]
Hint: Use def to declare variables in Groovy [OK]
Common Mistakes:
Using JavaScript keywords like var or let
Using := which is not Groovy syntax
Forgetting quotes around string values
3. What will be the output of this Groovy pipeline snippet?
def result = []
for (i in 1..3) {
result << i * 2
}
return result
medium
A. [2, 4, 6]
B. [1, 2, 3]
C. [3, 6, 9]
D. SyntaxError
Solution
Step 1: Understand the loop and range
The loop runs from 1 to 3 inclusive, so i takes values 1, 2, 3.
Step 2: Calculate each iteration's result
Each i is multiplied by 2 and added to the list: 2, 4, 6.
Final Answer:
[2, 4, 6] -> Option A
Quick Check:
Loop 1..3 * 2 = [2,4,6] [OK]
Hint: Multiply each loop value by 2 and collect in list [OK]
Common Mistakes:
Confusing range syntax with exclusive end
Forgetting to append to list
Expecting syntax error due to unfamiliar Groovy syntax
4. Identify the error in this Jenkins pipeline Groovy snippet: