Declarative versus Scripted Pipeline: what are the differences?
IntermediateConceptJenkins
Answer
Declarative has a fixed structure (pipeline { agent, stages, steps, post }), built-in options (when, parallel, post, options), syntax validation before running, and is easier to read and lint. Scripted is a node { } block of full Groovy, offering unlimited flexibility (loops, try/catch, dynamic stages) but with no structural validation and more ways to write unmaintainable code.
Our rule is declarative by default, with a script { } block for the occasional dynamic bit, and complex logic moved into a shared library.
pipeline {
agent { label 'linux' }
stages {
stage('Build') { steps { sh 'mvn -B -DskipTests package' } }
stage('Test') { steps { sh 'mvn -B test' } }
}
post { always { junit 'target/surefire-reports/*.xml' } }
}
More on Jenkins
What is Jenkins and how is its architecture organised?BeginnerWhat are executors, nodes and labels in Jenkins?IntermediateWhy should you not run builds on the Jenkins controller?BeginnerHow do inbound (JNLP/WebSocket) and SSH agents connect to the controller?IntermediateWhat is a Jenkinsfile and where does it live?BeginnerWhat does the `post` section in a declarative pipeline do?Beginner