0
0
Jenkinsdevops~10 mins

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

Choose your learning style9 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.