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.declarativeJenkinsfile.scripted
- Create a Pipeline job named
D-v-s-pipeline. - Under Pipeline → Definition select “Pipeline from SCM”.
- Point the job to the repository and branch
demo-1. - For the first run set the Script Path to
Jenkinsfile.declarative(so the job executes the Declarative pipeline).
- This Jenkinsfile demonstrates a simple Declarative Pipeline with one stage and a
postsection.
- 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 withskipDefaultCheckoutor when usingagent none). - The
postblock runs after the stages complete;alwaysexecutes regardless of success or failure.
post actions):
- The checkout stage appears automatically by default (unless Declarative checkout behavior is disabled).
- The
Echo Messagestage runs after checkout and lists repository files. - The
postactions 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.- Switch the job’s Script Path to
Jenkinsfile.scriptedand 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 thegitstep or other SCM-specific steps).
Jenkinsfile.scripted without explicit checkout:
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.- To make the Scripted pipeline perform the SCM checkout, add
checkout scminside thenodeblock (commonly inside a stage).
Jenkinsfile.scripted (with explicit checkout):
checkout scm:
- Automatic SCM checkout:
- Declarative: Performs checkout automatically by default and shows a
Declarative: Checkout SCMstage. This can be disabled with options likeskipDefaultCheckout. - Scripted: No automatic checkout; you must call
checkout scmexplicitly.
- Declarative: Performs checkout automatically by default and shows a
- 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
posthandling). - Scripted: Offers full Groovy control and flexibility, but requires manual handling for common tasks (checkout, structured post actions, restart behavior).
- Declarative: Enforces a higher-level structure that enables built-in features (automatic checkout, visual stages, structured
Links and references
- Jenkins Declarative Pipeline: https://www.jenkins.io/doc/book/pipeline/syntax/
- Jenkins Scripted Pipeline basics: https://www.jenkins.io/doc/book/pipeline/scripted/
- Blue Ocean (visual pipeline UI): https://www.jenkins.io/projects/blueocean/