Advanced Jenkins
Pipeline Structure and Scripted vs Declarative
Demo Declarative vs Scripted Pipeline
In this guide, we’ll compare Declarative and Scripted Jenkins pipelines using two separate Jenkinsfiles in the same Git repository. By the end, you’ll understand the key differences in configuration, SCM handling, and runtime behavior.
Repository Structure
Our repo declarative-vs-scripted-pipeline has two branches (demo-1 and demo-2) and two Jenkinsfiles:

- Jenkinsfile.declarative
- Jenkinsfile.scripted
Jenkinsfile.declarative
The Declarative Pipeline syntax includes an implicit checkout and a post section for cleanup:
pipeline {
agent any
stages {
stage('Echo Message') {
steps {
sh 'ls -ltr'
sh 'echo "This is executed within a DECLARATIVE Pipeline"'
}
}
}
post {
always {
sh 'echo "This will always run"'
sh 'rm -rf *'
}
}
}
Warning
The post { always { rm -rf * } } step will delete all files in the workspace. Use with caution.
Jenkinsfile.scripted
Scripted Pipelines rely on explicit SCM operations and Groovy control flow:
node {
try {
stage('Echo Message') {
sh 'ls -ltr'
sh 'echo "This is executed within a SCRIPTED Pipeline"'
}
} catch (err) {
echo "Failed: ${err}"
} finally {
sh 'echo "This will always run."'
// sh 'rm -rf *'
}
}
Note
Scripted Pipelines do not perform an automatic checkout scm. You must add it manually where needed.
1. Create the Pipeline Job
- In Jenkins, click New Item.
- Enter d-v-s-pipeline, select Pipeline, and click OK.

2. Configure the Declarative Pipeline
- Under Pipeline → Definition, choose Pipeline script from SCM.
- Enter your Git Repository URL, credentials (if any), and branch
demo-1. - In Script Path, set
Jenkinsfile.declarative. - Save and click Build.


3. Running the Declarative Pipeline
After the build starts, you’ll see these stages:
- Declarative: Checkout SCM (automatic)
- Echo Message
- Declarative: Post Actions

Sample log excerpt:
[Pipeline] checkout scm
> git config core.sparsecheckout # timeout=10
[Pipeline] sh
+ echo This is executed within a DECLARATIVE Pipeline
This is executed within a DECLARATIVE Pipeline
[Pipeline] sh
+ echo This will always run
This will always run
Declarative pipelines handle SCM checkout and cleanup automatically.
4. Switch to the Scripted Pipeline
- Return to Configure.
- Change Script Path to
Jenkinsfile.scripted. - Save and Build.
Log excerpt:
[Pipeline] stage (Echo Message)
[Pipeline] sh
+ ls -ltr
total 0
[Pipeline] sh
+ echo This is executed within a SCRIPTED Pipeline
This is executed within a SCRIPTED Pipeline
[Pipeline] sh
+ echo This will always run.
This will always run.
Notice: No checkout stage—only your custom steps run.
5. Add Checkout to Scripted Pipeline
Edit Jenkinsfile.scripted to include checkout scm inside the stage:
node {
try {
stage('Echo Message') {
checkout scm
sh 'ls -ltr'
sh 'echo "This is executed within a SCRIPTED Pipeline"'
}
} catch (err) {
echo "Failed: ${err}"
} finally {
sh 'echo "This will always run"'
// sh 'rm -rf *'
}
}
Commit and build. You’ll now see both checkout and shell steps:

Log excerpt:
[Pipeline] checkout scm
> git fetch --tags --force --progress http://... # timeout=10
[Pipeline] sh
+ ls -ltr
total 8
-rw-r--r-- 1 jenkins jenkins 311 Nov 10 12:29 Jenkinsfile.scripted
...
Summary of Differences
| Feature | Declarative Pipeline | Scripted Pipeline |
|---|---|---|
| SCM Handling | Implicit checkout scm | Requires manual checkout scm |
| Post-/Cleanup Actions | Built-in post {} blocks | try/catch/finally in Groovy |
| Stage Restart | Supported out of the box | Not supported |
| Syntax | Simplified, YAML-like structure | Full Groovy with scripting power |
For seamless SCM integration, stage restarts, and easier maintenance, Declarative Pipelines are generally recommended.
Links and References
Watch Video
Watch video content