Understanding the Differences Between Jenkins Scripted and Declarative Pipeline: A Comprehensive Guide with Real-World Examples

Praveen Dandu
5 min readMar 7, 2023

--

Jenkins is an open-source automation server that is widely used for continuous integration and continuous delivery (CI/CD). One of the key features of Jenkins is its support for pipelines, which enable developers to create automated workflows for building, testing, and deploying their applications. There are two types of pipelines in Jenkins: Scripted and Declarative. In this blog post, we will explore the differences between these two pipeline types, and provide code snippets to illustrate the key concepts.

Scripted Pipeline

Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on Groovy scripting language. In Scripted Pipeline, the entire workflow is defined in a single file called a Jenkinsfile. The Jenkinsfile is written in Groovy and is executed by the Jenkins Pipeline plugin. Scripted Pipeline provides a lot of flexibility and control over the workflow, but it can be more complex and verbose than Declarative Pipeline.

Here is an example of a simple Scripted Pipeline:

node {
stage('Build') {
// Build the application
sh 'mvn clean install'
}
stage('Test') {
// Run the tests
sh 'mvn test'
}
stage('Deploy') {
// Deploy the application
sh 'deploy.sh'
}
}

In this example, we define a pipeline that has three stages: Build, Test, and Deploy. Each stage is executed on a Jenkins node, and we use the sh step to run shell commands.

Declarative Pipeline

Declarative Pipeline is a more recent addition to Jenkins and provides a more structured and simpler syntax for defining pipelines. Declarative Pipeline is based on the Groovy programming language, but it uses a Groovy-based DSL (Domain-Specific Language) for pipeline configuration.. The main benefit of Declarative Pipeline is its readability and ease of use, as it is designed to be more intuitive and less verbose than Scripted Pipeline.

Here is an example of a simple Declarative Pipeline:

pipeline {
agent any
stages {
stage('Build') {
steps {
// Build the application
sh 'mvn clean install'
}
}
stage('Test') {
steps {
// Run the tests
sh 'mvn test'
}
}
stage('Deploy') {
steps {
// Deploy the application
sh 'deploy.sh'
}
}
}
}

In this example, we define a pipeline using the pipeline block, and we specify the agent to run the pipeline on any available node. We then define three stages using the stages block, and we use the steps block to define the individual steps for each stage.

Differences between Scripted and Declarative Pipeline

Now that we have seen examples of Scripted and Declarative Pipeline, let’s discuss the key differences between the two pipeline types:

  1. Syntax: The syntax for Scripted Pipeline is based on Groovy scripting language, while the syntax for Declarative Pipeline is also based on Groovy, but it uses a more structured and predefined format.
  2. Flexibility: Scripted Pipeline provides more flexibility and control over the pipeline workflow, while Declarative Pipeline provides a simpler and more structured syntax.
  3. Error handling: Scripted Pipeline allows for more granular error handling and recovery mechanisms, while Declarative Pipeline provides a simpler error handling mechanism that is more intuitive and easier to understand.
  4. Code reuse: Scripted Pipeline allows for more code reuse and modularity, while Declarative Pipeline is designed to be more self-contained and less reliant on external scripts and libraries.
  5. Readability: Declarative Pipeline is designed to be more readable and easier to understand, while Scripted Pipeline can be more complex and verbose.

Real-world example of how a DevOps team might use Jenkins to automate the build, test, and deployment of a web application:

  1. Build: The first stage in the pipeline is the build stage, where we compile the source code and package it into a deployable artifact. For example, we might use Maven to build a Java web application, and package the resulting WAR file.
  2. Test: The second stage is the test stage, where we run automated tests to ensure that the application is functioning correctly. We might use tools like JUnit for unit testing and Selenium for functional testing. We can also run performance tests using tools like JMeter.
  3. Deploy: The final stage is the deployment stage, where we deploy the application to a production environment. We might use tools like Ansible or Chef to configure the environment and deploy the application.

Here’s an example of a Declarative Pipeline that implements these stages:

pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
sh 'jmeter -n -t my_test_plan.jmx'
}
}
stage('Deploy') {
steps {
ansiblePlaybook credentialsId: 'my-ansible-credentials', inventory: 'my-inventory', playbook: 'deploy.yml'
}
}
}
}

In this example, we use Maven to build the application in the Build stage, and we run unit and performance tests in the Test stage. Finally, we use Ansible to deploy the application to a production environment in the Deploy stage.

This pipeline can be triggered automatically whenever changes are pushed to the source code repository, providing a seamless and automated way to build, test, and deploy the application.

An example of a Scripted Pipeline that implements the same stages as the Declarative Pipeline example:

node {
stage('Build') {
sh 'mvn clean package'
}
stage('Test') {
parallel (
"unit-tests": {
sh 'mvn test'
},
"performance-tests": {
sh 'jmeter -n -t my_test_plan.jmx'
}
)
}
stage('Deploy') {
withCredentials([usernamePassword(credentialsId: 'my-ansible-credentials', usernameVariable: 'ANSIBLE_USER', passwordVariable: 'ANSIBLE_PASSWORD')]) {
sh "ansible-playbook -i my-inventory -u ${ANSIBLE_USER} -k -v deploy.yml"
}
}
}

In this Scripted Pipeline, we also have three stages: Build, Test, and Deploy. In the Build stage, we use the sh step to run the mvn command to build the application. In the Test stage, we use the parallel step to run the unit and performance tests in parallel. Finally, in the Deploy stage, we use the withCredentials step to securely pass in the Ansible credentials, and we use the sh step to run the ansible-playbook command to deploy the application.

Overall, both Scripted and Declarative Pipeline provide a powerful and flexible way to automate the build, test, and deployment of applications in a DevOps environment. The choice between the two pipeline types largely depends on the specific needs of the development team, and factors such as flexibility, readability, and ease of use.

--

--

Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/