Difference between declarative and scripted pipeline

Rajat ChauhanRajat Chauhan
2 min read

In Jenkins, the terms "declarative" and "script" refer to two different types of syntax for defining Jenkins pipelines. Here’s a breakdown of each:

Declarative Pipeline

Declarative Pipeline syntax is a more structured and simplified way to define Jenkins pipelines. It uses a domain-specific language (DSL) that is designed to be more readable and easier to understand. It’s built on top of the Pipeline DSL and offers a more user-friendly experience, especially for those who may not be as familiar with Groovy scripting.

Key Features:

  • Structured Format: It uses a predefined structure and syntax. For example, it defines stages, steps, and other elements in a more organized manner.

  • Simplified Syntax: The declarative syntax is more concise and less error-prone.

  • Built-in Features: It provides built-in support for common pipeline features like environment variables, tools, and post-build actions.

  • Validation: The declarative syntax is validated before the pipeline runs, which helps catch errors early.

Example:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
            }
        }
    }
    post {
        always {
            echo 'This will always run after the pipeline completes.'
        }
    }
}

Scripted Pipeline

Scripted Pipeline syntax provides more flexibility but is also more complex. It’s essentially a Groovy script that defines the pipeline using a more traditional programming approach. This syntax allows for greater control and customization but requires a deeper understanding of Groovy and Jenkins internals.

Key Features:

  • Flexibility: Allows for more complex logic and custom behavior.

  • Groovy Script: The entire pipeline is defined using Groovy code, which can include loops, conditionals, and other programming constructs.

  • Manual Control: You have full control over the pipeline execution and can define steps in any way you choose.

Example:

node {
    stage('Build') {
        echo 'Building...'
    }
    stage('Test') {
        echo 'Testing...'
    }
    stage('Deploy') {
        echo 'Deploying...'
    }
    post {
        always {
            echo 'This will always run after the pipeline completes.'
        }
    }
}

Comparison

  • Readability: Declarative pipelines are generally easier to read and maintain due to their structured format. Scripted pipelines can be harder to understand, especially for those unfamiliar with Groovy.

  • Complexity: Scripted pipelines offer more flexibility and can handle more complex scenarios but may become cumbersome for simpler tasks.

  • Error Handling: Declarative pipelines often provide better error handling and validation before execution compared to scripted pipelines.

  • Customization: Scripted pipelines allow for more granular control and customization, which can be useful for advanced use cases.


Follow me:

LinkTree

1
Subscribe to my newsletter

Read articles from Rajat Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajat Chauhan
Rajat Chauhan

Rajat Chauhan is a skilled Devops Engineer, having experience in automating, configuring, deploying releasing and monitoring the applications on cloud environment. • Good experience in areas of DevOps, CI/CD Pipeline, Build and Release management, Hashicorp Terraform, Containerization, AWS, and Linux/Unix Administration. • As a DevOps Engineer, my objective is to strengthen the company’s applications and system features, configure servers and maintain networks to reinforce the company’s technical performance. • Ensure that environment is performing at its optimum level, manage system backups and provide infrastructure support. • Experience working on various DevOps technologies/ tools like GIT, GitHub Actions, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus and Grafana, and AWS EKS, DevOps, Jenkins. • Positive attitude, strong work ethic, and ability to work in a highly collaborative team environment. • Self-starter, Fast learner, and a Team player with strong interpersonal skills • Developed shell scripts (Bash) for automating day-to-day maintenance tasks on top of that have good python scripting skills. • Proficient in communication and project management with good experience in resolving issues.