Automating Web Application Deployment with Jenkins and GitHub

Automating Web Application Deployment with Jenkins and GitHub


Introduction

In the world of web development, the process of building, testing, and deploying applications can be time-consuming and error-prone if done manually. Automation tools like Jenkins and GitHub can streamline these tasks, making the process faster, more reliable, and easier to manage. This article will guide you through the steps to automate the deployment of a web application using Jenkins and GitHub, ensuring that new learners can easily understand and implement these concepts.

Project Overview

Our project aims to automate the building, testing, and deployment process of a web application using Jenkins and GitHub. Here’s what we’ll cover:

  1. Setting Up Jenkins

  2. Integrating Jenkins with GitHub

  3. Creating a Jenkins Pipeline

  4. Automating the Build, Test, and Deploy Stages

  5. Setting Up Notifications for Failures

Step 1 : Setting Up Jenkins

What is Jenkins?

Jenkins is an open-source automation server that helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery (CI/CD).

Installation

  1. Download Jenkins: Go to the Jenkins download page and download the installer for your operating system.

  2. Install Jenkins: Follow the installation instructions specific to your operating system.

  3. Start Jenkins: After installation, start Jenkins by running the appropriate command or starting the service.

  4. Access Jenkins: Open a web browser and go to http://localhost:8080. You’ll be prompted to enter an initial admin password found in the installation log.

Step 2 : Integrating Jenkins with GitHub

Setting Up GitHub Webhook

A webhook is a way for one application to send real-time data to another whenever a specific event occurs. In our case, we want GitHub to notify Jenkins whenever code changes are made to our repository.

  1. Create a GitHub Repository: If you don’t already have one, create a new repository on GitHub.

  2. Generate a Personal Access Token: In GitHub, go to Settings > Developer settings > Personal access tokens, and generate a new token with repo and admin:repo_hook permissions.

  3. Configure Webhook: In your GitHub repository, go to Settings > Webhooks, and add a new webhook with the following details:

    • Payload URL: http://your-jenkins-url/github-webhook/

    • Content type: application/json

    • Events: Select "Just the push event."

Connecting Jenkins to GitHub

  1. Install GitHub Plugin: In Jenkins, go to Manage Jenkins > Manage Plugins, search for "GitHub Integration Plugin," and install it.

  2. Add GitHub Credentials: Go to Manage Jenkins > Manage Credentials, and add a new credential with your GitHub personal access token.

Step 3 : Creating a Jenkins Pipeline

What is a Jenkins Pipeline?

A Jenkins pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. A pipeline defines the steps of your build process.

Creating a Pipeline

  1. New Item: In Jenkins, click on "New Item," enter a name for your pipeline, select "Pipeline," and click "OK."

  2. Pipeline Script: In the pipeline configuration page, scroll down to the Pipeline section and select "Pipeline script" from the Definition dropdown.

Jenkins Pipeline Script

The Jenkins pipeline script is written in Groovy, a language that is similar to Java but simpler to use. Below is a basic example of a pipeline script:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                // Add build steps here
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
                // Add test steps here
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
                // Add deployment steps here
            }
        }
    }
    post {
        failure {
            mail to: 'your-email@example.com',
                subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                body: "Something went wrong. Please check the Jenkins logs."
        }
    }
}

Step 4 : Automating the Build, Test, and Deploy Stages

Build Stage

The build stage compiles your application’s source code into executable code. Depending on your technology stack, this could involve different build tools (e.g., Maven for Java, npm for Node.js).

Example for a Node.js application:

stage('Build') {
    steps {
        sh 'npm install'
        sh 'npm run build'
    }
}

Test Stage

The test stage runs automated tests to ensure your application works as expected. This might include unit tests, integration tests, or end-to-end tests.

Example for a Node.js application:

stage('Test') {
    steps {
        sh 'npm test'
    }
}

Deploy Stage

The deploy stage deploys your application to a server or cloud environment. This could involve copying files to a server, running deployment scripts, or using deployment services.

Example for a Node.js application:

stage('Deploy') {
    steps {
        sh 'npm run deploy'
    }
}

Step 5 : Setting Up Notifications for Failures

To get notifications about failed builds or deployments, you can use Jenkins’ mailer plugin.

  1. Install Mailer Plugin: Go to Manage Jenkins > Manage Plugins, search for "Mailer," and install it.

  2. Configure SMTP Server: Go to Manage Jenkins > Configure System, scroll down to the "Extended E-mail Notification" section, and configure your SMTP server settings.

  3. Post-Failure Action: Add a post-failure action to your pipeline script to send an email notification.

Example of a post-failure action in the pipeline script:

post {
    failure {
        mail to: 'your-email@example.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something went wrong. Please check the Jenkins logs."
    }
}

Conclusion

By following these steps, you can automate the building, testing, and deployment process of your web application using Jenkins and GitHub. This not only saves time but also reduces the chances of human error, ensuring a more reliable and efficient workflow. Happy automating! 🚀


Additional Tips for New Learners

  1. Understand CI/CD : Continuous Integration (CI) is the practice of merging all developer working copies to a shared mainline several times a day. Continuous Delivery (CD) is the practice of automatically preparing code changes for a release to production. Understanding these concepts is key to successful automation.

  2. Learn Basic Groovy : Jenkins pipelines use Groovy, a language similar to Java. Basic knowledge of Groovy can help you customize your pipelines effectively.

  3. Start Small : Begin with simple automation tasks and gradually move to more complex workflows as you become more comfortable with Jenkins and GitHub.

  4. Use Jenkins Blue Ocean : Jenkins Blue Ocean provides a modern and intuitive user interface for Jenkins, making it easier to visualize and manage pipelines.

  5. Explore Plugins : Jenkins has a vast library of plugins that can extend its functionality. Explore and use plugins to enhance your automation process.

By implementing these practices, you can enhance your learning experience and become proficient in automating web application deployment using Jenkins and GitHub. Happy coding!