Differance between Jenkins Scripted Pipeline and Declarative Pipeline

Jenkins is an open-source automation server widely used for continuous integration and continuous delivery (CI/CD) of software projects. It allows developers to automate various tasks like building, testing and deploying their applications. Jenkins supports two primary types of pipeline syntax: scripted pipelines and declarative pipelines.

1. Scripted Pipeline:

Scripted Pipeline provides a more flexible and programmable way to define your pipeline using the Groovy scripting language.
It allows you to write custom logic and manipulate the flow of your pipeline stages. Here’s a basic example of a scripted pipeline:

node {
    stage('Build') {
        echo 'Building...'
        // Your build steps go here
    }

    stage('Test') {
        echo 'Testing...'
        // Your testing steps go here
    }

    stage('Deploy') {
        echo 'Deploying...'
        // Your deployment steps go here
    }
}

In Scripted Pipeline, you have complete control over the flow and can use conditional statements, loops, and other Groovy features to build complex workflows.

Benifits:

Scripted pipelines were the first version of the “pipeline-as-code” principle. They were designed as a DSL build with Groovy and provide an outstanding level of power and flexibility. However, this also requires some basic knowledge of Groovy, which sometimes isn’t desirable.

These kinds of pipelines have fewer restrictions on the structure. Also, they have only two basic blocks: “node” and “stage”. A “node” block specifies the machine that executes a particular pipeline, whereas the “stage” blocks are used to group steps that, when taken together, represent a separate operation. The lack of additional rules and blocks makes these pipelines quite simple to understand:

2. Declarative Pipeline:
Declarative Pipeline is designed to provide a more structured and opinionated way of defining pipelines. It uses a predefined syntax to describe the pipeline stages and their relationships. It is generally easier to read and understand, making it suitable for teams with varying levels of pipeline scripting expertise. Here’s a basic example of a declarative pipeline:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                // Your build steps go here
            }
        }

        stage('Test') {
            steps {
                echo 'Testing...'
                // Your testing steps go here
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying...'
                // Your deployment steps go here
            }
        }
    }
}

In Declarative Pipeline, you define stages and steps within those stages. It enforces a structured approach to pipeline creation.

Benifits:

Declarative pipelines are a more recent approach to the “pipeline-as-code” principle. They are quite easy to write and understand. The structure may seem to be a bit complex, but overall, it contains only a couple of basic sections. The “pipeline” block is the main block that contains the entire declaration of a pipeline. In this example, we’ll consider only “agent”“stages”, and “steps” sections:

  • pipeline – contains the whole pipeline
    • agent – defines the machine that will handle this pipeline
    • stages – declares the stages of the pipeline
      • steps – small operations inside a particular stage 

Which to Choose:
The choice between Scripted and Declarative Pipelines depends on your team’s requirements, skillset, and the complexity of your CI/CD process. Declarative Pipelines are recommended for simpler pipelines and teams with limited scripting knowledge, while Scripted Pipelines are more suitable for complex workflows that require custom logic and flexibility.

Both pipeline types have their merits, and you can even mix them within the same Jenkinsfile if needed. It’s important to consider factors such as maintainability, readability, and the specific needs of your projects when deciding which pipeline syntax to use.

Scripted Pipeline and Declarative Pipeline are two different ways to define pipelines in Jenkins. Here’s a the key differences between the two pipline:

1. Syntax and Structure:

  • Scripted Pipeline:
    • Scripted Pipeline uses Groovy scripting language to define the pipeline. This gives you full control over the pipeline’s flow and allows you to write custom logic using Groovy constructs like loops, conditionals, and functions.
    • The structure is more flexible and less opinionated. You need to define stages, steps, and other pipeline elements manually, which can lead to more complex scripts.
  • Declarative Pipeline:
    • Declarative Pipeline uses a structured DSL (Domain-Specific Language) that enforces a specific syntax. It’s designed to be more human-readable and easy to understand.
    • The structure is more opinionated and predefined. You declare the stages and steps within those stages using a specific syntax, making it suitable for straightforward pipelines.

2. Readability:

  • Scripted Pipeline:
    • While it offers more flexibility, Scripted Pipeline scripts can become complex and harder to read, especially as the pipeline logic becomes more intricate.
  • Declarative Pipeline:
    • Declarative Pipeline is designed to be highly readable and less prone to errors. It’s a good choice for teams with varying levels of scripting expertise.

3. Error Checking:

  • Scripted Pipeline:
    • In Scripted Pipeline, you need to manually handle error checking and validation. If there are syntax errors or issues in the script, they might not be caught until the pipeline is executed.
  • Declarative Pipeline:
    • Declarative Pipeline includes built-in syntax validation, which can catch errors before the pipeline is executed. This can help prevent common mistakes and improve pipeline reliability.

4. Ease of Use:

  • Scripted Pipeline:
    • Scripted Pipeline is more suitable for users who are comfortable with Groovy scripting and want more control over the pipeline’s behavior.
  • Declarative Pipeline:
    • Declarative Pipeline is designed for simplicity and ease of use. It’s a good choice for teams that want to quickly set up pipelines without diving into advanced scripting.

5. Flexibility:

  • Scripted Pipeline:
    • Scripted Pipeline is more flexible and can accommodate complex workflows and unique requirements.
  • Declarative Pipeline:
    • Declarative Pipeline is less flexible but provides a standardized way to define pipelines that adheres to best practices. It’s well-suited for straightforward use cases.

6. Best Practices:

  • Scripted Pipeline:
    • While it offers more control, Scripted Pipeline might require more effort to maintain and adhere to best practices due to the greater flexibility.
  • Declarative Pipeline:
    • Declarative Pipeline promotes best practices by enforcing a predefined structure. This can help ensure consistent and maintainable pipeline definitions.

In summary, Scripted Pipeline provides more flexibility and control over the pipeline’s behavior, making it suitable for complex scenarios. Declarative Pipeline offers a more structured and readable approach, making it great for simpler use cases and teams with varying levels of scripting expertise. The choice between the two depends on the specific requirements and preferences of your team and projects.

Hope you like this blog….
Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment