Bird
Raised Fist0
Jenkinsdevops~10 mins

Scripted vs declarative comparison in Jenkins - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Process Flow - Scripted vs declarative comparison
Start Pipeline
↓
Groovy Code Blocks
↓
Manual Flow Control
↓
Flexible, Complex
↓
Pipeline Runs
↓
Results & Logs
This flow shows how Jenkins pipelines start and split into scripted and declarative styles, highlighting their structure and execution.
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 TypeActionCode/StructureResult
1ScriptedStart pipelinenode { ... }Pipeline initialized
2ScriptedDefine stages manuallystage('Build') { ... }Stages set up
3ScriptedWrite Groovy code for stepsecho 'Building...'Build step runs
4ScriptedPipeline completesEnd of scriptPipeline finished
5DeclarativeStart pipelinepipeline { ... }Pipeline initialized
6DeclarativeDefine agent and stagesagent any, stages { ... }Stages structured
7DeclarativeDefine steps declarativelysteps { echo 'Building...' }Build step runs
8DeclarativePipeline completesEnd of pipeline blockPipeline finished
9BothCompare resultsN/ABoth run build step, differ in syntax and flexibility
💡 Both pipelines finish after running the build step; scripted uses Groovy code blocks, declarative uses structured syntax.
Status Tracker
VariableStartAfter Scripted Stage SetupAfter Scripted StepAfter Declarative Stage SetupAfter Declarative StepFinal
pipelineStateNot startedInitializedBuild step executedInitializedBuild step executedFinished
syntaxStyleN/AGroovy code blocksGroovy code blocksStructured blocksStructured blocksN/A
Key Moments - 2 Insights
Why does the scripted pipeline look like normal code while declarative looks like a config?
Scripted pipelines use Groovy code blocks allowing manual control (see execution_table rows 1-4), while declarative pipelines use a structured syntax with predefined sections (rows 5-8) for simplicity.
Can both pipelines do the same tasks?
Yes, both run the same build step (rows 3 and 7), but scripted pipelines allow more complex logic, while declarative pipelines focus on readability and standard structure.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the scripted pipeline run the build step?
AStep 3
BStep 2
CStep 7
DStep 5
💡 Hint
Check the 'Action' and 'Pipeline Type' columns for scripted pipeline build step.
At which step does the declarative pipeline define its stages?
AStep 2
BStep 3
CStep 6
DStep 8
💡 Hint
Look for 'Declarative' pipeline type and 'Define agent and stages' action.
If you want more manual control over the pipeline flow, which style would you choose?
ADeclarative
BScripted
CBoth are equal
DNeither
💡 Hint
Refer to variable_tracker 'syntaxStyle' and execution_table scripted pipeline steps.
Concept Snapshot
Scripted Pipeline:
- Uses Groovy code blocks
- Manual flow control
- Flexible, complex logic

Declarative Pipeline:
- Uses structured syntax
- Predefined sections (agent, stages)
- Simpler, readable

Both run pipeline steps but differ in syntax and control style.
Full Transcript
This visual execution compares scripted and declarative Jenkins pipelines. The flow starts with pipeline initialization, then splits into scripted pipelines using Groovy code blocks with manual control, and declarative pipelines using structured syntax with predefined sections. The execution table traces steps where each pipeline defines stages and runs a build step. Variables track pipeline state and syntax style changes. Key moments clarify why scripted looks like code and declarative like config, and confirm both can run the same tasks. The quiz tests understanding of when build steps run and which style offers more control. The snapshot summarizes key differences: scripted is flexible and code-like, declarative is simpler and structured.

Practice

(1/5)
1. Which statement best describes the main difference between scripted and declarative Jenkins pipelines?
easy
A. Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read.
B. Declarative pipelines use Groovy code and are flexible; scripted pipelines have a fixed structure and are easier to read.
C. Both scripted and declarative pipelines have the same syntax and flexibility.
D. Scripted pipelines are only for building Java projects; declarative pipelines are for all other projects.

Solution

  1. Step 1: Understand scripted pipeline nature

    Scripted pipelines use Groovy code, allowing more flexibility but requiring more coding skill.
  2. Step 2: Understand declarative pipeline nature

    Declarative pipelines have a fixed, simpler structure designed for easier writing and reading.
  3. Final Answer:

    Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read. -> Option A
  4. Quick Check:

    Scripted = flexible Groovy, Declarative = fixed structure [OK]
Hint: Remember: scripted = flexible code, declarative = fixed format [OK]
Common Mistakes:
  • Confusing which pipeline uses Groovy code
  • Thinking both pipelines have the same syntax
  • Believing scripted pipelines are only for Java
2. Which of the following is the correct way to start a declarative Jenkins pipeline script?
easy
A. node { stage('Build') { echo 'Building...' } }
B. def build() { echo 'Building...' }
C. build { echo 'Building...' }
D. pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } }

Solution

  1. Step 1: Identify declarative pipeline syntax

    Declarative pipelines start with the pipeline {} block and include agent and stages.
  2. Step 2: Compare options

    pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } correctly uses pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } which matches declarative syntax.
  3. Final Answer:

    pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } -> Option D
  4. Quick Check:

    Declarative starts with pipeline block [OK]
Hint: Declarative pipelines always start with 'pipeline { ... }' [OK]
Common Mistakes:
  • Using scripted syntax for declarative pipelines
  • Omitting the 'pipeline' block
  • Missing 'stages' or 'steps' keywords
3. Given this scripted pipeline snippet, what will be the output?
node {
  stage('Test') {
    echo 'Running tests'
  }
}
medium
A. Prints 'Running tests' during the 'Test' stage
B. Syntax error because 'stage' is not allowed in scripted pipelines
C. No output because 'node' block is empty
D. Pipeline fails due to missing 'pipeline' block

Solution

  1. Step 1: Analyze scripted pipeline structure

    The scripted pipeline uses node and stage blocks correctly with an echo step inside.
  2. Step 2: Determine output

    The pipeline will run the 'Test' stage and print 'Running tests' to the console.
  3. Final Answer:

    Prints 'Running tests' during the 'Test' stage -> Option A
  4. Quick Check:

    Scripted pipelines allow stage and echo inside node [OK]
Hint: Scripted pipelines use node and stage blocks freely [OK]
Common Mistakes:
  • Thinking 'stage' is only for declarative pipelines
  • Expecting a syntax error without 'pipeline' block
  • Assuming no output if node block is present
4. Identify the error in this declarative pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Build') {
      echo 'Building...'
    }
  }
}
medium
A. Incorrect use of 'pipeline' keyword
B. Missing 'agent' declaration
C. Missing 'steps' block inside the 'Build' stage
D. Using 'echo' instead of 'sh' command

Solution

  1. Step 1: Review declarative pipeline stage structure

    Each stage must contain a steps block that holds the commands to run.
  2. Step 2: Check the snippet

    The 'Build' stage directly contains echo without wrapping it inside steps {}, which is invalid.
  3. Final Answer:

    Missing 'steps' block inside the 'Build' stage -> Option C
  4. Quick Check:

    Declarative stages require steps block [OK]
Hint: Declarative stages always need a 'steps' block [OK]
Common Mistakes:
  • Omitting the 'steps' block inside stages
  • Confusing scripted and declarative syntax
  • Thinking 'echo' must be replaced by 'sh'
5. You want to create a Jenkins pipeline that runs a complex Groovy script for dynamic branching but also wants to keep some stages simple and readable. Which approach is best?
hard
A. Use a fully scripted pipeline for all stages to keep consistency.
B. Use a declarative pipeline with a scripted block inside for complex parts.
C. Use only declarative pipeline and avoid any Groovy scripting.
D. Write two separate pipelines and run them manually.

Solution

  1. Step 1: Understand pipeline flexibility needs

    Complex dynamic branching requires Groovy scripting flexibility, which scripted pipelines provide.
  2. Step 2: Combine declarative simplicity with scripted flexibility

    Declarative pipelines allow embedding scripted blocks for advanced logic, keeping most stages simple and readable.
  3. Final Answer:

    Use a declarative pipeline with a scripted block inside for complex parts. -> Option B
  4. Quick Check:

    Declarative + scripted block = best of both worlds [OK]
Hint: Embed scripted blocks inside declarative for complex logic [OK]
Common Mistakes:
  • Using only scripted pipeline losing readability
  • Avoiding scripted blocks in declarative pipelines
  • Running separate pipelines manually instead of combining
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