What is a script block in Groovy within Jenkins pipelines?
A script block is a section inside a Jenkins pipeline where you can write Groovy code directly. It allows you to run complex logic that is not supported by the declarative pipeline syntax.
Click to reveal answer
beginner
How do you define a script block in a Jenkins declarative pipeline?
You define a script block using the script { } keyword inside a stage or step. For example:
Script blocks let you use Groovy features like loops, conditionals, and variables that are not available in declarative syntax. This helps you write flexible and powerful pipeline logic.
Click to reveal answer
intermediate
Can you use variables declared inside a script block outside of it in Jenkins pipelines?
No, variables declared inside a script block are local to that block. To share data outside, you need to declare variables at the pipeline or stage level.
Click to reveal answer
intermediate
What happens if you put complex Groovy code outside a script block in a declarative pipeline?
The pipeline will fail to run because declarative syntax only supports limited Groovy expressions. Complex code must be inside a script block to execute properly.
Click to reveal answer
What keyword starts a script block in a Jenkins declarative pipeline?
Agroovy
Bscript
Cblock
Dcode
✗ Incorrect
The script keyword is used to start a script block where Groovy code can be written.
Where can you place a script block in a Jenkins declarative pipeline?
AInside a stage's steps section
BDirectly at the pipeline root
COnly inside environment variables
DOutside the pipeline block
✗ Incorrect
Script blocks must be inside a stage's steps section to run Groovy code.
What is a common reason to use script blocks in Jenkins pipelines?
ATo write shell commands
BTo define environment variables
CTo run complex Groovy logic not supported by declarative syntax
What happens if you try to use a variable declared inside a script block outside it?
AIt will cause a syntax error
BIt will be accessible globally
CIt will automatically convert to a string
DIt will not be accessible outside the script block
✗ Incorrect
Variables inside script blocks have local scope and are not accessible outside.
Which of these is NOT true about script blocks?
AThey can be used anywhere in the Jenkinsfile
BThey allow use of Groovy loops and conditionals
CThey help extend declarative pipeline capabilities
DThey require the <code>script</code> keyword
✗ Incorrect
Script blocks must be inside a stage's steps section; they cannot be used anywhere.
Explain what a script block is in Jenkins pipelines and why it is useful.
Think about how you add custom code inside a mostly fixed pipeline structure.
You got /4 concepts.
Describe the scope of variables declared inside a script block and how to share data outside it.
Consider where variables live and how to pass data between parts of the pipeline.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using script blocks in Jenkins pipelines?
easy
A. To define environment variables
B. To specify the agent where the pipeline runs
C. To run Groovy code for custom logic inside the pipeline
D. To declare pipeline stages
Solution
Step 1: Understand Jenkins pipeline structure
Jenkins pipelines use declarative syntax but allow Groovy code inside script blocks for flexibility.
Step 2: Identify the role of script blocks
script blocks let you write Groovy code for loops, conditions, and other logic not directly supported in declarative syntax.
Final Answer:
To run Groovy code for custom logic inside the pipeline -> Option C
Quick Check:
Script blocks = Groovy code inside pipeline [OK]
Hint: Script blocks run Groovy code inside steps [OK]
Common Mistakes:
Confusing script blocks with environment or agent declarations
Thinking script blocks define stages
Assuming script blocks are optional for all logic
2. Which of the following is the correct way to start a script block inside a declarative Jenkins pipeline?
easy
A. script { echo 'Hello' }
B. script: { echo 'Hello' }
C. script() { echo 'Hello' }
D. script => { echo 'Hello' }
Solution
Step 1: Recall correct syntax for script blocks
In declarative pipelines, script is a block keyword followed by curly braces containing Groovy code.
Step 2: Compare options
Only script { ... } is valid Groovy block syntax. Others use invalid punctuation or keywords.
Final Answer:
script { echo 'Hello' } -> Option A
Quick Check:
script block syntax = script { ... } [OK]
Hint: Use script { } with braces, no colons or arrows [OK]
Common Mistakes:
Using colons or arrows instead of braces
Adding parentheses after script keyword
Misplacing quotes or braces
3. What will be the output of this Jenkins pipeline snippet?
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
def sum = 0
for (i in 1..3) {
sum += i
}
echo "Sum is ${sum}"
}
}
}
}
}
medium
A. Sum is 6
B. Sum is 3
C. Sum is 0
D. Syntax error
Solution
Step 1: Analyze the for loop inside script block
The loop runs from 1 to 3 inclusive, adding each number to sum: 1 + 2 + 3 = 6.
Step 2: Understand echo output
The echo prints the string with sum value interpolated, so output is "Sum is 6".
Final Answer:
Sum is 6 -> Option A
Quick Check:
1+2+3=6 output = Sum is 6 [OK]
Hint: Sum 1 to 3 = 6; echo prints it [OK]
Common Mistakes:
Thinking loop ends at 2
Confusing sum initialization
Assuming syntax error due to script block
4. Identify the error in this Jenkins pipeline snippet using a script block:
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
for i in 1..3 {
echo "Number: ${i}"
}
}
}
}
}
}
medium
A. Script block must be outside steps
B. Incorrect use of echo inside script block
C. Agent declaration is invalid
D. Missing parentheses around for loop variable and range
Solution
Step 1: Check for loop syntax in Groovy
Groovy requires parentheses around the for loop variable and range: for (i in 1..3).
Step 2: Identify the error in given code
The code uses for i in 1..3 without parentheses, causing syntax error.
Final Answer:
Missing parentheses around for loop variable and range -> Option D
Quick Check:
Groovy for loop needs ( ) [OK]
Hint: Always use parentheses in Groovy for loops [OK]
Common Mistakes:
Omitting parentheses in for loops
Misplacing script block outside steps
Confusing echo usage inside script
5. You want to use a script block in Jenkins to create a map of numbers to their squares for numbers 1 to 4, then print each pair. Which code snippet correctly does this inside a script block?
hard
A. def squares = []
for (i in 1..4) {
squares.add(i * i)
}
squares.each { echo "${it} squared" }
B. def squares = [:]
for (i in 1..4) {
squares[i] = i * i
}
squares.each { k, v -> echo "${k} squared is ${v}" }
C. def squares = [:]
for i in 1..4 {
squares[i] = i * i
}
squares.each { k, v -> echo "${k} squared is ${v}" }
D. def squares = [:]
for (i in 1..4) {
squares[i] = i ^ 2
}
squares.each { k, v -> echo "${k} squared is ${v}" }
Solution
Step 1: Check map creation and loop syntax
def squares = [:]
for (i in 1..4) {
squares[i] = i * i
}
squares.each { k, v -> echo "${k} squared is ${v}" } correctly creates an empty map with [:] and uses parentheses in the for loop.
Step 2: Verify square calculation and iteration
def squares = [:]
for (i in 1..4) {
squares[i] = i * i
}
squares.each { k, v -> echo "${k} squared is ${v}" } uses i * i for squares and iterates map with each { k, v -> ... } to echo key-value pairs.
Final Answer:
def squares = [:]
for (i in 1..4) {
squares[i] = i * i
}
squares.each { k, v -> echo "${k} squared is ${v}" } -> Option B
Quick Check:
Map with for (i in 1..4) and i*i is correct [OK]
Hint: Use [:] for map, for (i in range), i*i for squares [OK]
Common Mistakes:
Using list [] instead of map [:]
Omitting parentheses in for loop
Using ^ for power instead of *
Incorrect map iteration syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.