Declarative VS Scripted pipeline

Declarative VS Scripted pipeline

In Jenkins, a pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. There are two types of pipelines in Jenkins: Declarative and Scripted.

Declarative Pipeline is a more recent feature of Jenkins that provides a more simplified and efficient syntax for defining pipelines as code. It is based on a Domain Specific Language (DSL) that is written in Groovy and uses a more structured and concise syntax. Declarative pipelines are easier to read and understand, and they can be more flexible and powerful than traditional scripted pipelines.

Scripted Pipeline is the older, more traditional way of defining pipelines in Jenkins. It is a more powerful and flexible way of defining pipelines, but it requires you to write code in the Groovy programming language. Scripted pipelines can be more complex to write and maintain than declarative pipelines.

Both Declarative and Scripted pipelines can be used to create Jenkins pipelines, and the choice between the two will depend on your specific needs and preferences.

Declarative Pipeline

In Jenkins, a Declarative Pipeline is a way to define a Jenkins pipeline using a more structured and simplified syntax. Declarative Pipelines are defined using a Groovy-based DSL (Domain Specific Language).

A Declarative Multi-Stage Pipeline is a Declarative Pipeline that is divided into multiple stages. Each stage represents a phase in the delivery pipeline, and it specifies the actions that need to be executed in that phase. For example, a multi-stage pipeline might have stages for building, testing, and deploying a software application.

Here is an example of a Declarative Multi-Stage Pipeline:

pipeline {
    agent {
        docker {
            image 'maven:3.6.3-jdk-11'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh './deploy.sh'
            }
        }
    }
}

In this example, the pipeline has three stages: Build, Test, and Deploy. The Build stage runs the Maven clean and package goals, the Test stage runs the Maven test goal, and the Deploy stage runs a script called deploy.sh.

Scripted Pipeline

In Jenkins, a Scripted Pipeline is a way to define a Jenkins pipeline using the Groovy programming language. A Scripted Pipeline is typically written in a Jenkinsfile, which is checked into source control.

A Scripted Multi-Stage Pipeline is a Scripted Pipeline that is divided into multiple stages. Each stage represents a phase in the delivery pipeline, and it specifies the actions that need to be executed in that phase. For example, a multi-stage pipeline might have stages for building, testing, and deploying a software application.

Here is an example of a Scripted Multi-Stage Pipeline:

node {
    stage('Build') {
        sh 'mvn -B -DskipTests clean package'
    }
    stage('Test') {
        sh 'mvn test'
    }
    stage('Deploy') {
        sh './deploy.sh'
    }
}

In this example, the pipeline has three stages: Build, Test, and Deploy. The Build stage runs the Maven clean and package goals, the Test stage runs the Maven test goal, and the Deploy stage runs a script called deploy.sh.

Scripted pipelines offer more flexibility and power than Declarative Pipelines, but they can be more difficult to write and maintain. You should choose the type of pipeline that is best suited to your needs based on your specific requirements and expertise.