Project 1 - Day 80 Automating the deployment process of a web application using Jenkins and GitHub

Project 1 - Day 80 Automating the deployment process of a web application using Jenkins and GitHub

Description

The project aims to automate the building, testing, and deployment process of a web application using Jenkins and GitHub. The Jenkins pipeline will be triggered automatically by GitHub webhook integration when changes are made to the code repository. The pipeline will include stages such as building, testing, and deploying the application, with notifications and alerts for failed builds or deployments.

Steps

Log in to the AWS console.

Create an EC2 instance and launch it.

Now connect to the instance using the SSH client

Go to the download folder, where the .pem file is placed and open the terminal in the same location, and paste the SSH.

This will connect your terminal to the EC2 instance

Let's install Jenkins on this EC2 instance

https://www.jenkins.io/doc/book/installing/linux/

sudo apt update
sudo apt install openjdk-17-jre -y
java -version
curl -fsSL https://pkg.jenkins.io/debian/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 binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Install the Docker service by using the below command

sudo apt install docker.io -y

Let's check all the services, we installed till now

java --version
sudo systemctl status jenkins
sudo systemctl status docker

# Add the ubuntu/jenkins user to docker group and reboot to get previlaged access
$ sudo usermod -aG docker $USER

Run some docker commands in the terminal

# Check the docker images
$ docker images

Allow ports 8080 for Jenkins and 8000 for docker from a security group > inbound rules

Now, Copy the Public IP of the EC2 machine and paste it to the browser to access the Jenkins portal as "3.87.221.248:8080"

We need an Administrator Password to unlock this. So navigate to the directory given in the Jenkins portal and paste it to continue

“sudo 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

Provide the username, password, full name and email details

Click on Save and Continue

Click on Save and Finish

Welcome to Jenkins's home page

We will create a Jenkins project from the GitHub

Click on the + New item from the Dashboard

Add a name to the project and select it as a Freestyle project

Add a description to the project and select it as a GitHub project

https://github.com/rjthapaa/node-todo-cicd.git

In Source Code Management, select Git and Add Repository URL and Credentials if any

Specify the branch, if any

Leave the rest to default

Moving to the Build Step, select Execute Shell and write the following command to build a Docker image from the Docker file and from the image we will run a container.

Click on Apply and Save

Always check the ports exposed in the docker file in Gitrepo before using the docker run command to create a container to use the right port.

Now, Click on Build Now. And the build will be started, in the build history.

Note: It is important to add our Jenkins to the docker group, so docker commands can be executable on Jenkins

$ sudo usermod -aG docker jenkins
$ sudo reboot

After getting success, In the browser, search for

<Public_IP_of_EC2:8000>

Enabling GitScm polling in Jenkins project

We will be deploying the project using GitScm polling so that whenever the developer commits their code in GitHub, after every commit, it should reflect in the live web application.

Let's reconfigure the Jenkins project again using GitScm polling

Build Triggers: GitHub hook trigger for GITScm polling

Save and apply the changes to the project

To make this GITScm polling active we need to add an plugin

(Dashboard > Manage Jenkins > Plugins > Available Plugins > GitHub Integration).

Integrating GitHub repository with Jenkins

Generate the public and private RSA in the EC2 instance by using the below command

ssh-keygen

We have generated public and private RSA in the directory cd .ssh

Now, Goto GitHub Console > Settings > SSH and GPG Keys > New SSH Key

And paste the public key: id_rsa.pub

Title: Jenkins and Github intergration

Open the “id_rsa.pub” file and copy the content. (Public Key)

Back to Jenkins Dashboard

Open the project > Source Code Management > Credentials > Add > Jenkins

Kind = SSH username with the private key

Description = GitHub and Jenkins

username = Ubuntu

Private key > Enter directly > Add > Paste the Private key

Where to find the Private key: EC2 > cd .ssh > cat id_rsa

Integration between Jenkins and GitHub is done.

Click on the credentials now and select Ubuntu

Click on Apply and save.

We have successfully integrated GitHub and Jenkins with RSA and also activated the webhooks in the GitHub repository for the Enkinss project.

Adding Webhooks to GitHub repo

Now, we need to go to the GitHub project repo > Setting > Webhooks > Add Webhook

Payload URL: http://<public_ip_of_ec2>:8080/github-webhook/

Select send me everything checkbox

Click on Add Webhook

Check the Webhooks

Now, Save the configured project.

Do some changes in the code and push it to GitHub, this will automatically run a pipeline, and the new code will be Live.

Open the project in the GitHub repo and commit with some changes in the code

Notice the Jenkins dashboard the commit has triggered the project to build