Boost Your Continuous Integration with Jenkins Pipelines: A Comprehensive Guide to Different Types with Practical Examples

  1. Simple pipeline:

This is the most basic type of pipeline, which consists of a single stage.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
    }
}

  1. Parallel pipeline:

This type of pipeline executes multiple stages in parallel.

pipeline {
    agent any
    stages {
        stage('Build and Test') {
            parallel {
                stage('Build') {
                    steps {
                        sh 'mvn clean install'
                    }
                }
                stage('Unit Test') {
                    steps {
                        sh 'mvn test'
                    }
                }
                stage('Integration Test') {
                    steps {
                        sh 'mvn integration-test'
                    }
                }
            }
        }
    }
}

  1. Conditional pipeline:

This type of pipeline executes different stages based on a condition.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Deploy') {
            when {
                branch 'master'
            }
            steps {
                sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
            }
        }
    }
}

  1. Declarative pipeline:

This type of pipeline uses a declarative syntax, which makes it easier to read and write.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
            }
        }
    }
    post {
        always {
            sh 'rm -rf target'
        }
        success {
            slackSend channel: '#notifications', message: 'Build successful!'
        }
        failure {
            slackSend channel: '#notifications', message: 'Build failed!'
        }
    }
}

  1. Scripted pipeline:

This type of pipeline uses a scripted syntax, which allows for more flexibility and customization.

node {
    stage('Build') {
        sh 'mvn clean install'
    }
    stage('Test') {
        sh 'mvn test'
    }
    stage('Deploy') {
        sshagent(['my-ssh-key']) {
            sh 'scp target/my-app.war user@prod-server:/opt/tomcat/webapps'
        }
    }
    post {
        always {
            sh 'rm -rf target'
        }
        success {
            slackSend channel: '#notifications', message: 'Build successful!'
        }
        failure {
            slackSend channel: '#notifications', message: 'Build failed!'
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *