Understanding Jenkins Pipeline

Understanding Jenkins Pipeline

A Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. A pipeline consists of a series of stages and steps that automate the process of building, testing, and deploying code.

Creating a new Jenkins Pipeline

Add a new item and select item type as Pipeline.

Details in a Jenkins pipeline Job

Pipeline Syntax - Pipeline syntax is used to generate steps code for different types of step:

  • Click on Pipeline Syntax
  • In the new Page, provide the sample step and the detail of the step and then generate Pipeline script.
  • Add the generated script in the pipeline step at the required place

JenkinsFile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline, which is a set of automated steps used to build, test, and deploy software. Jenkins Pipelines are used in continuous integration and continuous delivery (CI/CD) processes.

Example of a basic Jenkins Pipeline

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
            }
        }
    }
}        

Understanding different Keywords in Jenkins Pipeline

Basic flow in a pipeline are:

  • stages: define the sequence of stage that the pipeline will execute. Stages section can have multiple stage
  • Stage?Each stage represents a major phase in your build, test, and deployment process.Each stage must be declared with a?stage?block.?A stage can have?multiple stage within itselfeach stage has a name and?steps, parallel, and when?keyword to define the flow of execution within that stage.

  • Steps? - Define the specific?steps?that the pipeline should execute.?Steps can include shell commands, script execution, and other operations.
  • When - To make the execution of command conditional

  • post: Defines actions to take after the build, including cleaning up the workspace and printing success or failure messages.
  • Parallel: To execute multiple stage in parallel inside a step

Understanding the details in a pipeline

Example of a detailed Pipeline


pipeline {
    agent any

    options {
        timeout(time: 1, unit: 'HOURS')
        buildDiscarder(logRotator(numToKeepStr: '5'))
    }

    parameters {
        string(name: 'SERVER', defaultValue: 'NODE1', description: 'Please provide server details')
        choice(name: 'ENVIRONMENT', choices: ['dev', 'qa', 'prod'], description: 'Deployment environment')
    }

    environment {
        MAVEN_HOME = tool 'Maven 3.6.3'
        PATH = "${MAVEN_HOME}/bin:${env.PATH}"
    }

    triggers {
        cron('H/15 * * * *')
    }

    tools {
        jdk 'JDK 11'
    }

    stages {
        stage('Approval') {
            steps {
                input 'Do you want to proceed with the build?'
            }
        }

        stage('Checkout') {
            steps {
                echo 'Checking out code...'
                // git url: 'https://github.com/your-repository/project.git', branch: 'main'
            }
        }

        stage('Build') {
            when {
                expression {
                    return params.ENVIRONMENT != 'prod'
                }
            }
            steps {
                echo 'Building the project...'
                sh 'mvn clean compile'
            }
        }

        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'mvn test'
            }
        }

        stage('Package') {
            steps {
                echo 'Packaging the project...'
                sh 'mvn package'
            }
        }

        stage('Deploy') {
            parallel {
                stage('Deploy to Dev') {
                    when {
                        expression {
                            return params.ENVIRONMENT == 'dev'
                        }
                    }
                    steps {
                        echo 'Deploying to development environment...'
                        // Deployment steps go here
                    }
                }
                stage('Deploy to QA') {
                    when {
                        expression {
                            return params.ENVIRONMENT == 'qa'
                        }
                    }
                    steps {
                        echo 'Deploying to QA environment...'
                        // Deployment steps go here
                    }
                }
                stage('Deploy to Prod') {
                    when {
                        expression {
                            return params.ENVIRONMENT == 'prod'
                        }
                    }
                    steps {
                        echo 'Deploying to production environment...'
                        // Deployment steps go here
                    }
                }
            }
        }
    }

    post {
        always {
            echo 'Cleaning up...'
        }
        success {
            echo 'Pipeline succeeded!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
}        

Parallel Execution in pipeline

Parallel execution in Jenkins Pipeline can be managed using the parallel directive. This allows multiple stages or steps to run simultaneously, which can significantly reduce the overall pipeline execution time.

stage('Test') {
    parallel {
        stage('Unit Tests') {
            steps {
                echo  'unit test'
            }
        }
        stage('Functional Tests') {
            steps {
               echo  'functional test'
            }
        }
    }
}        



要查看或添加评论,请登录

Nitin Joshi的更多文章

社区洞察

其他会员也浏览了