In this section, we will explore how to set up a Continuous Delivery (CD) pipeline using Jenkins. Continuous Delivery is a software development practice where code changes are automatically prepared for a release to production. It expands upon Continuous Integration by deploying all code changes to a testing environment and/or a production environment after the build stage.

Key Concepts

  1. Continuous Delivery (CD): The practice of automatically preparing code changes for release to production.
  2. Pipeline: A series of automated processes that take software from version control to production.
  3. Stages: Different phases in a pipeline, such as build, test, and deploy.
  4. Jenkinsfile: A text file that contains the definition of a Jenkins Pipeline and is checked into source control.

Steps to Set Up a CD Pipeline

  1. Define the Jenkinsfile

The Jenkinsfile is the cornerstone of your CD pipeline. It defines the stages and steps of your pipeline. Here is an example of a simple Jenkinsfile for a CD pipeline:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                // Add your build steps here
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
                // Add your test steps here
            }
        }
        stage('Deploy to Staging') {
            steps {
                echo 'Deploying to Staging...'
                // Add your deployment steps here
            }
        }
        stage('Deploy to Production') {
            steps {
                input message: 'Approve deployment to production?', ok: 'Deploy'
                echo 'Deploying to Production...'
                // Add your production deployment steps here
            }
        }
    }
}

  1. Create a New Pipeline Job in Jenkins

  1. Open Jenkins Dashboard.
  2. Click on "New Item".
  3. Enter a name for your pipeline and select "Pipeline".
  4. Click "OK".

  1. Configure the Pipeline

  1. In the pipeline configuration page, scroll down to the "Pipeline" section.
  2. Select "Pipeline script from SCM".
  3. Choose your SCM (e.g., Git) and provide the repository URL.
  4. Specify the path to your Jenkinsfile (e.g., Jenkinsfile).

  1. Triggering the Pipeline

You can configure triggers to automatically start the pipeline when certain events occur, such as:

  • SCM Polling: Jenkins will periodically check the repository for changes.
  • Webhooks: Configure your version control system to notify Jenkins of changes.

  1. Running the Pipeline

Once configured, you can manually trigger the pipeline by clicking "Build Now" on the pipeline job page. Jenkins will execute the stages defined in the Jenkinsfile.

Practical Example

Let's create a more detailed Jenkinsfile for a Node.js application:

pipeline {
    agent any

    environment {
        NODE_ENV = 'production'
    }

    stages {
        stage('Build') {
            steps {
                script {
                    def nodeVersion = '14.x'
                    sh "nvm install ${nodeVersion}"
                    sh "nvm use ${nodeVersion}"
                    sh 'npm install'
                }
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
        stage('Deploy to Staging') {
            steps {
                sh 'npm run deploy:staging'
            }
        }
        stage('Deploy to Production') {
            steps {
                input message: 'Approve deployment to production?', ok: 'Deploy'
                sh 'npm run deploy:production'
            }
        }
    }
}

Explanation

  • Environment Variables: NODE_ENV is set to 'production'.
  • Build Stage: Installs the specified Node.js version and dependencies.
  • Test Stage: Runs the tests.
  • Deploy to Staging: Deploys the application to the staging environment.
  • Deploy to Production: Waits for manual approval before deploying to production.

Exercises

Exercise 1: Create a Simple CD Pipeline

  1. Create a new repository and add a simple application (e.g., a "Hello World" Node.js app).
  2. Write a Jenkinsfile that includes build, test, and deploy stages.
  3. Configure a Jenkins pipeline job to use this repository.
  4. Trigger the pipeline and observe the stages.

Exercise 2: Add Manual Approval

  1. Modify the Jenkinsfile to include a manual approval step before deploying to production.
  2. Trigger the pipeline and approve the deployment when prompted.

Solutions

Solution to Exercise 1

  1. Create a repository and add a simple Node.js app.
  2. Example Jenkinsfile:
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
        stage('Deploy to Staging') {
            steps {
                sh 'npm run deploy:staging'
            }
        }
        stage('Deploy to Production') {
            steps {
                input message: 'Approve deployment to production?', ok: 'Deploy'
                sh 'npm run deploy:production'
            }
        }
    }
}
  1. Configure the Jenkins pipeline job as described in the steps above.
  2. Trigger the pipeline and observe the stages.

Solution to Exercise 2

  1. Modify the Jenkinsfile to include the input step before the production deployment.
  2. Trigger the pipeline and approve the deployment when prompted.

Summary

In this section, we covered the basics of setting up a Continuous Delivery pipeline using Jenkins. We learned how to define a Jenkinsfile, create and configure a pipeline job, and trigger the pipeline. We also explored a practical example and provided exercises to reinforce the concepts. In the next section, we will delve into automating deployments in more detail.

© Copyright 2024. All rights reserved