What is the basic structure of a Jenkins pipeline script using Groovy?
A Jenkins pipeline script typically starts with the pipeline block, containing agent to specify where to run, and stages with multiple stage blocks defining steps.
Click to reveal answer
beginner
How do you define a variable in Groovy within a Jenkins pipeline?
You define a variable using def keyword, for example: def myVar = 'Hello'. Variables can be used to store values during the pipeline execution.
Click to reveal answer
intermediate
What is the purpose of the script block in a declarative Jenkins pipeline?
The script block allows you to write imperative Groovy code inside a declarative pipeline, enabling complex logic that is not supported directly by declarative syntax.
Click to reveal answer
beginner
How do you write a simple conditional statement in Groovy within a Jenkins pipeline?
Use the standard Groovy if statement, for example: if (env.BRANCH_NAME == 'main') { echo 'On main branch' }.
Click to reveal answer
beginner
What is the difference between sh and bat steps in Jenkins pipelines?
sh runs shell commands on Unix/Linux agents, while bat runs batch commands on Windows agents. Both are used to execute system commands from Groovy scripts.
Click to reveal answer
Which keyword is used to define a variable in Groovy within a Jenkins pipeline?
Adef
Bvar
Clet
Dconst
✗ Incorrect
In Groovy, variables are defined using the def keyword.
What does the agent block specify in a Jenkins pipeline?
AThe pipeline stages
BThe machine or environment where the pipeline runs
CThe steps to execute
DThe pipeline triggers
✗ Incorrect
The agent block tells Jenkins where to run the pipeline or stage.
How do you write a conditional statement in Groovy to check if the branch is 'main'?
Aif (BRANCH_NAME === 'main') { ... }
Bif BRANCH_NAME = 'main' then ...
Cif (env.BRANCH_NAME == 'main') { ... }
Dif [ $BRANCH_NAME == 'main' ]
✗ Incorrect
Groovy uses if (condition) { } syntax and environment variables are accessed via env.
What is the purpose of the script block in a declarative pipeline?
ATo write imperative Groovy code inside declarative syntax
BTo define environment variables
CTo specify the agent
DTo declare pipeline stages
✗ Incorrect
The script block allows complex Groovy code that declarative syntax does not support.
Which step should you use to run a shell command on a Linux agent in Jenkins pipeline?
Apowershell
Bbat
Ccmd
Dsh
✗ Incorrect
sh runs shell commands on Unix/Linux agents.
Explain how to structure a simple Jenkins pipeline using Groovy syntax.
Think about the main blocks that organize the pipeline.
You got /4 concepts.
Describe how to use variables and conditional statements in Jenkins pipeline Groovy scripts.
Focus on variable declaration and simple if conditions.
You got /4 concepts.
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: