Skip to main content
This lesson compares Declarative and Scripted Jenkins pipelines using a small demo repository. You’ll create a single Pipeline job and run it twice: once using a Declarative Jenkinsfile and once using a Scripted Jenkinsfile to observe the differences in behavior (notably SCM checkout and post/finally execution). Repository and branch
  • Repository: declarative-vs-scripted-pipeline (hosted on the demo Gitea server)
  • Branch: demo-1
  • The branch contains two Jenkinsfiles:
    • Jenkinsfile.declarative
    • Jenkinsfile.scripted
Setup: create the Pipeline job
  1. Create a Pipeline job named D-v-s-pipeline.
  2. Under Pipeline → Definition select “Pipeline from SCM”.
  3. Point the job to the repository and branch demo-1.
  4. For the first run set the Script Path to Jenkinsfile.declarative (so the job executes the Declarative pipeline).
Jenkinsfile.declarative
  • This Jenkinsfile demonstrates a simple Declarative Pipeline with one stage and a post section.
Key points about the Declarative pipeline
  • By default, Declarative pipelines perform an SCM checkout before the first stage and display that as a separate stage (Declarative: Checkout SCM) in Blue Ocean or the classic stage view. This behavior can be disabled (for example with skipDefaultCheckout or when using agent none).
  • The post block runs after the stages complete; always executes regardless of success or failure.
Representative pipeline log excerpt for Declarative (automatic checkout + post actions):
Notes from the example:
  • The checkout stage appears automatically by default (unless Declarative checkout behavior is disabled).
  • The Echo Message stage runs after checkout and lists repository files.
  • The post actions run at the end and, in this example, remove workspace files.
Declarative pipelines are opinionated: they give a structured syntax (pipeline {}, stages, post) which enables features like automatic checkout, easier visualization in Blue Ocean, and stage restart support.
Scripted pipeline (no automatic checkout)
  • Switch the job’s Script Path to Jenkinsfile.scripted and run the job.
  • Scripted pipelines do not perform SCM checkout automatically. If you want source code present in the workspace, you must explicitly call checkout scm (or use the git step or other SCM-specific steps).
Example Jenkinsfile.scripted without explicit checkout:
Representative scripted pipeline log (no checkout performed; workspace empty):
If your Scripted pipeline needs the repository files, remember to add an explicit checkout (e.g., checkout scm). Forgetting to do so will leave the workspace empty.
Add explicit checkout in Scripted pipeline
  • To make the Scripted pipeline perform the SCM checkout, add checkout scm inside the node block (commonly inside a stage).
Updated Jenkinsfile.scripted (with explicit checkout):
Representative log excerpt after adding checkout scm:
Summary of differences demonstrated
  • Automatic SCM checkout:
    • Declarative: Performs checkout automatically by default and shows a Declarative: Checkout SCM stage. This can be disabled with options like skipDefaultCheckout.
    • Scripted: No automatic checkout; you must call checkout scm explicitly.
  • Restart-from-stage:
    • Declarative: Supports restarting from a specific stage (with stage checkpoints and appropriate plugins/features).
    • Scripted: Does not support restart-from-stage in the same way as Declarative.
  • Structure and opinionation:
    • Declarative: Enforces a higher-level structure that enables built-in features (automatic checkout, visual stages, structured post handling).
    • Scripted: Offers full Groovy control and flexibility, but requires manual handling for common tasks (checkout, structured post actions, restart behavior).
Differences at a glance Links and references That’s all for this lesson.

Watch Video