Bird
Raised Fist0
Jenkinsdevops~10 mins

When to use scripted over declarative in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - When to use scripted over declarative
Start Pipeline
↓
Choose Pipeline Type
↓
Declarative
The flow shows starting a Jenkins pipeline, choosing between declarative for simple, standard cases, or scripted for complex, custom logic needs.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
}
A simple declarative pipeline that runs a build stage printing a message.
Process Table
StepPipeline TypeReason for ChoiceAction TakenResult
1DeclarativeSimple linear flowDefine stages and stepsPipeline runs with clear structure
2DeclarativeStandard CI/CD tasksUse built-in syntaxEasy to read and maintain
3ScriptedNeed complex logicWrite Groovy code with loops/conditionsFlexible control over flow
4ScriptedCustom error handlingUse try-catch blocksHandle errors precisely
5DeclarativeLimited scripting neededUse scripted blocks inside declarativeBalance simplicity and flexibility
6End--Pipeline completes or fails based on logic
💡 Pipeline finishes after all stages or error handling completes
Status Tracker
VariableStartAfter Step 1After Step 3Final
pipelineTypeundefineddeclarativescriptedscripted
stagenoneBuildCustom LogicCustom Logic
errorHandledfalsefalsetruetrue
Key Moments - 2 Insights
Why choose scripted pipeline for complex logic?
Scripted pipelines allow full Groovy code use, enabling loops, conditions, and error handling not easily done in declarative (see execution_table rows 3 and 4).
Can declarative pipelines include scripted parts?
Yes, declarative pipelines support scripted blocks for complex steps, balancing simplicity and flexibility (see execution_table row 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which step shows using try-catch for error handling?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Reason for Choice' column for 'Custom error handling'
At which step does the pipeline type change to scripted?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Pipeline Type' column for the first mention of 'scripted'
If you want easy to maintain pipelines with standard tasks, which pipeline type is best?
ADeclarative
BNeither
CScripted
DBoth equally
💡 Hint
Refer to execution_table rows 1 and 2 about simple linear flow and standard tasks
Concept Snapshot
Jenkins pipelines come in two types:
- Declarative: simple, readable, good for standard CI/CD
- Scripted: flexible, Groovy-based, for complex logic
Use declarative for most cases; use scripted when you need loops, conditions, or custom error handling.
Declarative can include scripted blocks for extra flexibility.
Full Transcript
This visual execution shows when to use scripted versus declarative Jenkins pipelines. Start by choosing pipeline type. Declarative pipelines are best for simple, standard tasks with clear stages and steps. Scripted pipelines allow full Groovy coding for complex logic like loops and error handling. You can mix scripted blocks inside declarative pipelines for flexibility. The execution table traces steps choosing pipeline type, reasons, actions, and results. Variable tracking shows pipelineType changing from declarative to scripted when complex logic is needed. Key moments clarify why scripted is chosen for custom logic and that declarative can include scripted parts. The quiz tests understanding of these choices referencing the execution table. The snapshot summarizes the main points for quick recall.

Practice

(1/5)
1. Which situation is best suited for using a scripted Jenkins pipeline instead of a declarative one?
easy
A. When you want to use built-in syntax for parallel stages only
B. When you want a simple, easy-to-read pipeline with fixed stages
C. When you need complex loops and conditional logic that declarative can't handle
D. When you want to avoid writing any Groovy code

Solution

  1. Step 1: Understand pipeline types

    Declarative pipelines are simpler and use a fixed syntax, while scripted pipelines allow full Groovy code control.
  2. Step 2: Match use case to pipeline type

    Complex loops and dynamic conditions require scripted pipelines because declarative syntax is limited.
  3. Final Answer:

    When you need complex loops and conditional logic that declarative can't handle -> Option C
  4. Quick Check:

    Complex logic = scripted [OK]
Hint: Choose scripted for complex logic, declarative for simple flow [OK]
Common Mistakes:
  • Thinking declarative supports all Groovy features
  • Using scripted for simple pipelines unnecessarily
  • Confusing parallel stage support as exclusive to scripted
2. Which of the following is the correct way to start a scripted pipeline in Jenkins?
easy
A. node { stage('Build') { echo 'Hello' } }
B. pipeline { agent any; stages { stage('Build') { steps { echo 'Hello' } } } }
C. stages { stage('Build') { steps { echo 'Hello' } } }
D. build { stage('Build') { echo 'Hello' } }

Solution

  1. Step 1: Identify scripted pipeline syntax

    Scripted pipelines use Groovy code inside a node block, not the pipeline block.
  2. Step 2: Check each option

    node { stage('Build') { echo 'Hello' } } uses node { stage('Build') { echo 'Hello' } } which is correct scripted syntax.
  3. Final Answer:

    node { stage('Build') { echo 'Hello' } } -> Option A
  4. Quick Check:

    Scripted pipelines start with node block [OK]
Hint: Scripted pipelines start with node block, declarative with pipeline [OK]
Common Mistakes:
  • Using declarative pipeline syntax for scripted pipelines
  • Omitting the node block in scripted pipelines
  • Confusing stages block as scripted 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: 3
C. Syntax error due to for loop
D. No output because echo is invalid here

Solution

  1. Step 1: Understand scripted pipeline loop

    The for loop runs from 1 to 3, printing each count with echo.
  2. Step 2: Predict output

    Each iteration prints "Count: 1", "Count: 2", and "Count: 3" on separate lines.
  3. Final Answer:

    Count: 1\nCount: 2\nCount: 3 -> Option A
  4. Quick Check:

    Loop prints each count line [OK]
Hint: For loops in scripted pipelines print each iteration output [OK]
Common Mistakes:
  • Assuming only last iteration prints
  • Thinking for loop syntax is invalid in scripted
  • Believing echo cannot be used inside node
4. Identify the error in this scripted pipeline snippet:
node {
  stage('Build') {
    echo 'Building...'
  }
  stage('Test')
    echo 'Testing...'
}
medium
A. stage keyword cannot be used in scripted pipelines
B. Incorrect use of echo command inside node
C. node block must be inside stage blocks
D. Missing braces {} around the 'Test' stage block

Solution

  1. Step 1: Check stage syntax in scripted pipeline

    Each stage block must have braces {} to group its commands.
  2. Step 2: Identify missing braces

    The 'Test' stage lacks braces around echo, causing syntax error.
  3. Final Answer:

    Missing braces {} around the 'Test' stage block -> Option D
  4. Quick Check:

    All stage blocks need braces in scripted [OK]
Hint: Always use braces {} for stage blocks in scripted pipelines [OK]
Common Mistakes:
  • Omitting braces for single commands in stages
  • Thinking stage keyword is invalid in scripted
  • Misplacing node and stage blocks
5. You want to create a Jenkins pipeline that dynamically creates stages based on a list of environments (dev, test, prod). Which pipeline type should you use and why?
hard
A. Declarative pipeline, because it supports dynamic stage creation easily
B. Scripted pipeline, because it allows loops and dynamic stage generation with Groovy
C. Declarative pipeline, because scripted pipelines cannot handle multiple stages
D. Neither, Jenkins does not support dynamic stages

Solution

  1. Step 1: Understand dynamic stage requirements

    Creating stages dynamically requires looping and conditional logic.
  2. Step 2: Match pipeline type to dynamic behavior

    Scripted pipelines allow Groovy loops to generate stages dynamically; declarative pipelines are static.
  3. Final Answer:

    Scripted pipeline, because it allows loops and dynamic stage generation with Groovy -> Option B
  4. Quick Check:

    Dynamic stages = scripted pipeline [OK]
Hint: Use scripted pipelines for dynamic or complex stage creation [OK]
Common Mistakes:
  • Assuming declarative supports dynamic stages easily
  • Believing scripted pipelines cannot handle multiple stages
  • Thinking Jenkins disallows dynamic pipeline structures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard