Jenkins CICD With GitHub Integration

Project-2

Jenkins CICD With GitHub Integration

Project Overview

Our project, titled "Jenkins CI/CD with GitHub Integration," has a primary objective of automating the end-to-end development and deployment process. Utilizing a technology stack that includes Jenkins, GitHub, Docker, and Node.js, we've established a robust CI/CD pipeline. This pipeline seamlessly integrates with our GitHub repository, following the GitFlow branching strategy, and is triggered by webhooks whenever code changes are pushed. Our Dockerfile plays an important role in containerizing our application for enhanced portability and consistency across environments. The CI/CD pipeline includes stages for building, testing, and deploying our web application, ultimately ensuring code quality and expediting the release cycle. Implementing this CI/CD pipeline promises faster releases, heightened code reliability, and an overall more efficient development cycle, composed to enhance our web application deployment process significantly.

Pre-requisites

1. A GitHub account to store the source code.

2. One Ec2 Instance

3. Jenkins, Docker and Docker Compose are installed.

4. Docker registry to store the Docker-versioned images.

Now, let's dive in-->

Step 1: Get the source code of the Node js App

Fork this repo: https://github.com/Akshay-aan/node-js-cicd.git

Step 2: Make the server ready for deployment

Create an ec2 instance, one as the master node for deployment purposes.

Instance can be t2-micro, 1 CPU, 1 GiB Memory, and ports 8080,8000,80,22 are open to allow incoming traffic

Step3:Install Docker

sudo apt-get update

sudo apt install docker.io

$ sudo usermod -aG docker $USER

$ sudo reboot

docker -v

Step 4: Setup the server to use Jenkins

Install Java

1. Jenkins requires Java to run, so first install Java -->

$ sudo apt-get update

$ sudo apt install openjdk-11-jre

$ java -version

Install Jenkins

Just copy these commands and paste them onto your terminal.

$ 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 jenkins

$ jenkins --version

Start Jenkins

sudo systemctl enable jenkins

sudo systemctl start jenkins

sudo systemctl status jenkins

Access your Jenkins on http://Localhost[EC2 instance public ip]:8080

Step5:Install Jenkins Plugin

Install the GitHub integration plugin in Jenkins.

Go to the Manage Jenkins option.click on plugins

click on available plugins and install the plugin

Step6: Add public key into git hub

Before creating a freestyle job for our node js app we need to integrate our github with Jenkins. For that go to the GitHub account setting

Get the public key by (id_rsa.pub)performing the same steps given below.

Take the public key that was previously encrypted and paste it into the designated key box.

We have successfully added the key

Step7:Add Webhook into the GitHub Repository

Go to the settings of the repository

Go to the webhooks-->add webhook

Provide the password of your GitHub account

The green tick indicates that the integration between your Jenkins and Github is done successfully

Step8: Create a project to get the source code from github

Either click on New Item or create a job

Create a Freestyle job

add private key in Jenkins credentials.

Now again go to your ec2 instance cd .ssh and then open the id_rsa in read-only mode using command cat id_rsa.

copy the above private key and paste into the private key section.

Allow GitHub hook trigger

Click on Save and apply. and then Build Now.

Here we got the source code from GitHub in the path of the Jenkins workspace

/var/lib/jenkins/workspace/node-js-app

give permissions

sudo usermod -aG docker Jenkins

sudo chmod 777 /var/lib/jenkins/workspace/node-js-app

sudo systemctl restart Jenkins

Step9:Create a Dockerfile for node-js-app

FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]

Now we need to create container using Dockerfile

Step 10: The code was built successfully Now run the app using the docker command through Jenkins.

Go to our node-js-app pipeline -->Go to build Step--> Execute Shell -->apply-->save

Console Output

Code was built and run using the docker command through Jenkins.

Here We Add the webhook trigger to the GitHub repository. So as we commit the changes to the code in our repository, the webhook automatically triggers the Jenkins CICD pipeline.

Here we deploy the app successfully

Done!

Summary

Our project's primary objective is to implement a streamlined Continuous Integration/Continuous Deployment (CI/CD) pipeline using Jenkins, GitHub, and Docker. This pipeline automates the building, testing, and deployment of our web application. We've integrated GitHub with Jenkins through a webhook for immediate feedback, and we've containerized our application using a Dockerfile for consistency and scalability. This automation ensures rapid and error-free deployments, enhancing our development workflow and overall efficiency.

Thank you for reading and being a part of this project. Your support means a lot. Stay with us for more exciting updates ahead!

Akshay Nazirkar