Scripted and a Declarative pipeline in Jenkins

Scripted Pipeline

A scripted pipeline in Jenkins is written in Groovy and allows for more flexibility and control.

 node {
    stage('Checkout') {
        // Checkout code from version control
        checkout scm
    }

    stage('Build') {
        // Build the project
        echo 'Building...'
        sh 'make build'
    }

    stage('Test') {
        // Run tests
        echo 'Testing...'
        sh 'make test'
    }

    stage('Deploy') {
        // Deploy the application
        echo 'Deploying...'
        sh 'make deploy'
    }
}

Declarative Pipeline

A declarative pipeline provides a more structured and simpler syntax.

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                // Checkout code from version control
                checkout scm
            }
        }

        stage('Build') {
            steps {
                // Build the project
                echo 'Building...'
                sh 'make build'
            }
        }

        stage('Test') {
            steps {
                // Run tests
                echo 'Testing...'
                sh 'make test'
            }
        }

        stage('Deploy') {
            steps {
                // Deploy the application
                echo 'Deploying...'
                sh 'make deploy'
            }
        }
    }
}

Key Differences

  1. Syntax:

    • Scripted: Uses Groovy scripting with node { ... } blocks.

    • Declarative: Uses a more structured and predefined syntax with pipeline { ... }.

  2. Flexibility:

    • Scripted: Offers more flexibility and control but can be more complex.

    • Declarative: Easier to read and write, enforces a more structured approach.

  3. Error Handling:

    • Scripted: Requires manual implementation of error handling and post-build actions.

    • Declarative: Provides built-in mechanisms for error handling and post-build actions using post { ... } blocks.

Thank you all,

Kindly follow Amos Koech