Pipeline

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.

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'
    }
}

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.

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'
            }
        }
    }
}

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

  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.

Last updated