A scripted pipeline is a way to write Jenkins pipelines using Groovy code, giving full control over the flow and logic of the build process.
Click to reveal answer
intermediate
How does a scripted pipeline differ from a declarative pipeline?
Scripted pipelines use code to define steps and logic, allowing complex conditions and loops, while declarative pipelines use a simpler, structured syntax with limited flexibility.
Click to reveal answer
beginner
Why does scripted pipeline offer more flexibility?
Because it lets you write custom Groovy code, use loops, conditions, and functions freely, enabling complex workflows that declarative pipelines can't easily handle.
Click to reveal answer
intermediate
Give an example of a feature that scripted pipelines support but declarative pipelines struggle with.
Scripted pipelines can easily implement dynamic parallel stages or complex error handling using Groovy code, which is hard or impossible in declarative pipelines.
Click to reveal answer
beginner
What is a real-life analogy for scripted pipelines' flexibility?
It's like having a blank notebook where you can write any story you want, versus a coloring book with fixed shapes to fill in (declarative pipeline).
Click to reveal answer
What language is used to write Jenkins scripted pipelines?
AGroovy
BPython
CJavaScript
DRuby
✗ Incorrect
Jenkins scripted pipelines are written using Groovy code.
Which pipeline type offers more control over complex logic?
AMultibranch pipeline
BDeclarative pipeline
CScripted pipeline
DNone of the above
✗ Incorrect
Scripted pipelines allow writing custom code for complex logic, offering more control.
Which of these is easier to write but less flexible?
AScripted pipeline
BDeclarative pipeline
CBoth are equally flexible
DNeither
✗ Incorrect
Declarative pipelines have a simpler syntax but less flexibility.
Why might you choose a scripted pipeline over a declarative one?
ATo write complex loops and conditions
BBecause scripted pipelines run faster
CTo use simple, fixed steps
DBecause declarative pipelines are deprecated
✗ Incorrect
Scripted pipelines let you write complex loops and conditions easily.
Which pipeline type is like a blank notebook for writing any story?
APipeline as code
BDeclarative pipeline
CMultibranch pipeline
DScripted pipeline
✗ Incorrect
Scripted pipeline is like a blank notebook allowing any story (logic) to be written.
Explain why scripted pipelines offer more flexibility than declarative pipelines in Jenkins.
Think about how coding freely compares to using a fixed template.
You got /3 concepts.
Describe a scenario where using a scripted pipeline is better than a declarative pipeline.
Consider when simple fixed steps are not enough.
You got /3 concepts.
Practice
(1/5)
1. Why do scripted pipelines in Jenkins offer more flexibility compared to declarative pipelines?
easy
A. Because they only support predefined stages without customization.
B. Because they have a simpler syntax with fewer features.
C. Because they allow using loops, conditions, and custom error handling.
D. Because they automatically generate pipeline code without user input.
Solution
Step 1: Understand scripted pipeline features
Scripted pipelines allow writing code with loops, conditions, and error handling, giving more control.
Step 2: Compare with declarative pipelines
Declarative pipelines have a simpler, fixed structure and less flexibility for complex logic.
Final Answer:
Because they allow using loops, conditions, and custom error handling. -> Option C
Quick Check:
Scripted pipelines = flexible code control [OK]
Hint: Look for options mentioning loops and conditions for flexibility [OK]
Common Mistakes:
Confusing simplicity with flexibility
Thinking declarative pipelines allow all custom code
Assuming scripted pipelines auto-generate code
2. Which of the following is the correct syntax to start a scripted pipeline block in Jenkins?
easy
A. node {
B. stage {
C. pipeline {
D. scripted {
Solution
Step 1: Identify scripted pipeline block syntax
Scripted pipelines start with node { to define the workspace and steps.
Step 2: Eliminate other options
pipeline { is for declarative pipelines; stage { defines a stage; scripted { is invalid syntax.
Final Answer:
node { -> Option A
Quick Check:
Scripted pipeline starts with node block [OK]
Hint: Scripted pipelines start with 'node {' block [OK]
Common Mistakes:
Using 'pipeline {' which is declarative syntax
Confusing 'stage {' as pipeline start
Assuming 'scripted {' is valid syntax
3. What will be the output of this scripted pipeline snippet?
node {
for (int i = 1; i <= 3; i++) {
echo "Count: ${i}"
}
}
medium
A. Count: 1\nCount: 2\nCount: 3
B. Count: 0\nCount: 1\nCount: 2
C. Syntax error due to for loop
D. No output because echo is invalid
Solution
Step 1: Analyze the for loop range
The loop runs from i=1 to i=3 inclusive, so it iterates 3 times.
Step 2: Check the echo output each iteration
Each iteration prints "Count: " followed by the current i value, producing lines for 1, 2, and 3.
Final Answer:
Count: 1\nCount: 2\nCount: 3 -> Option A
Quick Check:
Loop 1 to 3 prints counts 1,2,3 [OK]
Hint: Check loop start and end values carefully [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Thinking echo is invalid in scripted pipeline
Assuming syntax error in for loop
4. Identify the error in this scripted pipeline snippet:
node {
if (env.BRANCH_NAME == 'main')
echo 'Deploying to production'
else
echo 'Deploying to staging'
}
medium
A. node block cannot contain if statements
B. Missing curly braces {} around if and else blocks
C. echo command cannot be used inside if
D. Incorrect use of env variable syntax
Solution
Step 1: Check if-else syntax in scripted pipeline
In scripted pipelines, if and else blocks must use curly braces {} when spanning multiple lines or statements.
Step 2: Identify missing braces
The snippet lacks braces around the echo statements, which can cause syntax errors or unexpected behavior.
Final Answer:
Missing curly braces {} around if and else blocks -> Option B
Quick Check:
If-else blocks need braces in scripted pipelines [OK]
Hint: Always use braces {} for if-else blocks in scripted pipelines [OK]
Common Mistakes:
Assuming env.BRANCH_NAME syntax is wrong
Thinking echo can't be inside if
Believing node block forbids if statements
5. You want to retry a build step up to 3 times if it fails, using a scripted pipeline. Which snippet correctly implements this retry logic?
hard
A. node {
for (int i=0; i<3; i++) {
sh 'run-build.sh'
}
}
B. node {
while (true) {
sh 'run-build.sh'
}
}
C. node {
try {
sh 'run-build.sh'
} catch (err) {
retry(3)
}
}
D. node {
retry(3) {
sh 'run-build.sh'
}
}
Solution
Step 1: Understand Jenkins retry step
The retry(n) { ... } block retries the enclosed steps up to n times on failure automatically.
Step 2: Evaluate each option
node {
retry(3) {
sh 'run-build.sh'
}
} correctly uses retry(3) around the build command. node {
for (int i=0; i<3; i++) {
sh 'run-build.sh'
}
} runs the command 3 times without retry on failure. node {
try {
sh 'run-build.sh'
} catch (err) {
retry(3)
}
} misuses retry inside catch. node {
while (true) {
sh 'run-build.sh'
}
} loops infinitely.
Final Answer:
node {
retry(3) {
sh 'run-build.sh'
}
} -> Option D
Quick Check:
Use retry(n) block for automatic retries [OK]
Hint: Use retry(n) block to retry steps automatically [OK]
Common Mistakes:
Using loops instead of retry block
Placing retry inside catch block incorrectly
Creating infinite loops without exit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.