Day 80 : Project 1 - Jenkins Freestyle Project

Day 80 : Project 1 - Jenkins Freestyle Project

In today's fast-paced development environment, automating the build, test, and deployment processes is crucial for ensuring efficiency and reliability. Manual processes can be time-consuming and error-prone, leading to delayed releases and potential issues in production. To address these challenges, our project focuses on leveraging Jenkins and GitHub to automate the entire workflow for a web application. This blog will guide you through the project, highlighting key components and steps involved in creating an automated pipeline.

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:
  • If you haven’t already, create a GitHub repository to host the web application’s code.

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

Install and Configure Jenkins:

  • Create a new EC2 instance. Install Docker and Jenkins using the below User Data file.
#!/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
  • Once created, connect using your preferred method and verify Docker and Jenkins are up and running.

Install Required Jenkins Plugins:

  • Now, Copy the Public Ip of the machine and paste it to the browser to access the Jenkins portal. As,

  • We need an Administrator Password to unlock this. For that, go to the provided highlighted path in the upper screenshot.

    “cat /var/lib/Jenkins/secrets/initialAdminPassword”

Paste this password in the “Administrator Password” Column.

  • Now Click on, “Install Suggested Plugins”

  • This will now install the suggested plugins. As

  • Now, Jenkins will ask us to create the First Admin User.

  • Add the fields accordingly.

  • The Jenkins homepage will look like this,

Create Jenkins Pipeline:

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

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

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

  • 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.

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

  • Lastly, we will set up our Pipeline in the “Build Steps” section. Let’s add a new build step, with the type ‘Execute shell’ and add the below instructions.
echo "--------------------------------Clone Code Stage--------------------------------"
cd /var/lib/jenkins/workspace/DevOps Project 1
git clone https://github.com/PrathmeshV2001/django-notes-app
echo "--------------------------------Build Stage --------------------------------"
cd /var/lib/jenkins/workspace/DevOps Project 1/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--------------------------------"

  • Apply the changes, and save the Pipeline.

Run the Pipeline:

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

  • The GitHub changes will automatically trigger the Jenkins pipeline.

  • Monitor the pipeline’s progress in the Jenkins Pipeline dashboard.

Test the Pipeline:

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

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

Configure Notifications and Alerts:

  • Click on Manage Jenkins->Configure System. Here scroll down to the Email Notification section. Now enter the details as the following image

  • If you use Gmail with MFA enabled, follow the below steps:

  • Create a custom app in your Gmail security settings.

  1. Log in to Gmail with your account

  2. Navigate to https://security.google.com/settings/security/apppasswords

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

  4. It will give you 16 chars token.

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

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

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

  • Here is successful pipeline build.

Conclusion :

By automating the build, test, and deployment processes with Jenkins and GitHub, you can significantly streamline your development workflow. This project not only saves time but also ensures that your web application is consistently built and tested with every change, leading to more reliable and faster releases.

Get started today by setting up your Jenkins server and configuring your GitHub repository. With the power of automation, you can focus more on developing features and less on managing deployments. Happy coding!

I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .

thank you : )