Day 80 - Automate Your CI/CD Pipeline with Jenkins and GitHub 🚀✨

Nilkanth MistryNilkanth Mistry
5 min read

Welcome to Day 80 of the #90DaysOfDevOps challenge! 🚀 Today, we are diving into our first DevOps project, where we will automate the building and deployment process of a web application using Jenkins and GitHub. This project will empower you to efficiently manage the development lifecycle and streamline the deployment process. So, let’s get started! 🌟

Project Description 📄

Project Aim: Automate the building and deployment process of a web application using Jenkins and GitHub.

Project Overview: In this project, we will create a Jenkins pipeline that will be triggered automatically by GitHub whenever changes are made to the code repository. The pipeline will include multiple stages such as cloning the code from GitHub, building the application, and deploying the application on the target server. Additionally, we’ll set up notifications and alerts to inform us in case of any failed builds or deployments.


Hands-on Project: Jenkins Freestyle Project 🛠️

1. Set up GitHub Repository 📂

  1. If you haven’t already, create a GitHub repository to host the web application’s code.

  2. Push your web application code to the repository. I’ll use my django-notes-app repository.

2. Install and Configure Jenkins 🖥️

  1. Create a new EC2 instance:

    • Launch an EC2 instance in AWS. ☁️

    • Select an appropriate instance type (e.g., t2.micro for testing).

    • Use the below User Data script to install Docker and Jenkins automatically:

    #!/bin/bash

    echo "Docker installation 🐳"
    echo "------------------------------------------------------------------"
    sudo apt update
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt update
    sudo apt install -y docker-ce docker-ce-cli containerd.io
    sudo systemctl start docker
    sudo systemctl enable docker
    sudo usermod -aG docker $USER

    echo "Jenkins installation 🔧"
    echo "------------------------------------------------------------------"
    sudo apt update
    sudo apt install -y openjdk-17-jre
    curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
      /usr/share/keyrings/jenkins-keyring.asc > /dev/null
    echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
      https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
      /etc/apt/sources.list.d/jenkins.list > /dev/null
    sudo apt-get update
    sudo apt-get install -y jenkins

  1. Connect to your instance:

    • Connect to your EC2 instance using your preferred method (e.g., SSH). 🖧

    • Verify Docker and Jenkins are up and running by checking their status:

        sudo systemctl status docker
        sudo systemctl status jenkins
      

3. Install Required Jenkins Plugins 🔌

  1. Access the Jenkins dashboard in your web browser using the instance's public IP address (e.g., http://<ec2-instance-public-ip>:8080).

  2. Install the suggested plugins during the initial setup.

  3. Create your user account and log in with the newly created username and password.

4. Create Jenkins Pipeline 📜

  1. In the Jenkins dashboard, click on “New Item.” 🆕

  2. Enter a name for your pipeline and select “Freestyle” as the project type.

  3. Under “Source Code Management,” choose “Git” and provide your GitHub Repository URL.

  4. In the “Build Triggers” section, select Poll SCM to trigger the Jenkins Pipeline when changes are discovered. We’ll schedule it to run a scan every minute (* * * * *). 🕒

  5. Select “Delete workspace before build starts” to ensure there are no residual files every time we run the Jenkins Pipeline.

  6. Set up your Pipeline in the “Build Steps” section:

    • Add a new build step, with the type ‘Execute shell’ and add the below instructions:
    echo "Clone Code Stage 📥 --------------------------------"
    cd /var/lib/jenkins/workspace/day80
    git clone https://github.com/LondheShubham153/django-notes-app.git
    echo "Build Stage 🔨 --------------------------------"
    cd /var/lib/jenkins/workspace/day80/django-notes-app
    docker build . -t django-notes-app
    echo "Image created 🖼️"
    echo "Deploy Stage 🚀 --------------------------------"
    docker stop django-notes-app || true
    docker rm django-notes-app || true
    docker run -d -p 8000:8000 --name django-notes-app django-notes-app
    echo "Container is created and running 🏃‍♂️"

  1. Apply the changes, and save the Pipeline. 💾

5. Run the Pipeline 🚀

  1. Make changes to your web application code and push them to the GitHub repository.

  2. The GitHub changes will automatically trigger the Jenkins pipeline. 🔄

  3. Monitor the pipeline’s progress in the Jenkins Pipeline dashboard. 📊

6. Test the Pipeline 🧪

  1. Check the job’s console output, where we can see if the Docker container has been successfully created.

  2. Navigate to http://<ec2-instance-public-ip>:8000 and access your newly deployed app. 🌐

7. Configure Notifications and Alerts 📧

  1. Click on Manage Jenkins -> Configure System. ⚙️

  2. Scroll down to the Email Notification section.

  3. Enter the details as shown in the image below (example setup):

  4. If you use Gmail with MFA enabled, follow these steps:

    • Create a custom app in your Gmail security settings.

    • Log in to Gmail with your account.

    • Navigate to Google App Passwords.

    • In ‘select app’ choose ‘custom’, give it an arbitrary name and press generate. 🆕

    • It will give you a 16-character token.

    • Use the token as a password in combination with your full Gmail account, and two-factor authentication will not be required.

  5. Use Jenkins’ built-in features to set up email notifications for pipeline status (success/failure) in the “Post-build Actions”. 📬

  6. Modify the Pipeline Build Steps to ensure the job fails, and wait for an email notification to land in your inbox. 📩


Conclusion 🎉

Congratulations on completing Day 80 of the #90DaysOfDevOps challenge! Today, we successfully set up a Jenkins pipeline to automate the building and deployment of a web application using GitHub. We configured the Jenkins pipeline to be triggered automatically using Poll SCM whenever changes are pushed to the GitHub repository. Additionally, we set up notifications and alerts to stay informed about the pipeline’s status. 📈

Tomorrow, we’ll take our DevOps journey further and explore a new project where we’ll upgrade this project by using a Jenkins Declarative Pipeline instead. Stay tuned! 🚀

I hope you learned something from this blog. If you have, don’t forget to follow and click the clap 👏 button below to show your support 😄. Subscribe to my blogs so that you won’t miss any future posts. 📰

If you have any questions or feedback, feel free to leave a comment below. Thanks for reading and have an amazing day ahead! 🌟

1
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement