TO-DO APPLICATION IN 3 WAYS.
Deploying a To-Do App with Jenkins: A Step-by-Step Guide & Accelerate Your Web Application Deployment with a Jenkins Pipeline Triggered by GitHub Webhook Integration.
Introduction:
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.
Task 1
Step 1:
Go to EC2 console and Create an EC2 instance.
Check whether EC2 instance is running and copy the IPv4 address of the instance.
Connect to the EC2 instance using Puttygen and Putty with the help of IPv4 and Keys generated at the time of instance launch.
Step 2: Install jenkins on AWS EC2 Ubuntu instance.
1: Install Java
Jenkins requires the Java Runtime Environment (JRE).
To install OpenJDK 11, run:
$ java
$ sudo apt install openjdk-11-jdk-headless -y
2: Add Jenkins Repository
1. Start by importing the GPG key. The GPG key verifies package integrity but there is no output.$
$ sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
2. Add the Jenkins software repository to the source list and provide the authentication key:
$ 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
3: Install Jenkins
1. Update the system repository
$ sudo apt-get update
2. Install Jenkins by running:
$ sudo apt-get install jenkins
To check if Jenkins is installed and running, run the following command:
$ sudo systemctl enable jenkins
$ sudo systemctl status jenkins
4: Generate SSH Keys (Public and Private):
Generate the SSH keys for integrating your Jenkins project with your git repository. Use ssh-keygen command to create public and private key.
$ ssh-keygen
$ cd .ssh
$ ls
Step 3:
1: Go to Security Groups and Edit inbound rules:
For Jenkins we used port 8080 and to deploy our application we use port 8000.
To add port 8080 to an instance, you need to allow traffic on port 8080 in the instance’s security group and configure the instance’s firewall to allow traffic on port 8080 and same with the port 8000.
2: To verify Jenkins server is running :
Browse < instance-public-IP:8080 > it will open the Jenkins dashboard.
3. We need an Administrator Password to unlock this. Go to terminal and use below command for password.
cat /var/lib/jenkins/secrets/initialAdminPassword
Paste the above password in Administrator Password and click on Continue
4. Select Install suggested plugins:
This will install all the suggested plugins in the Jenkins server automatically or you can select plugins to install.
5. Plugins start installing automatically:
When we select the above option all the plugins start installing automatically.
5. Create first admin user:
Now fill this page to create a user and click on Save and Continue.
6. Jenkins setup is completed and Jenkins is ready to use.
7. Jenkins Dashboard:
Step 4: Create a freestyle project
- In the Jenkins dashboard, Click on the ‘New Item’ button on the left sidebar then give your project a name and select “Freestyle project” as the project type.
2. Configure the Job:
Click on Configure, Add a description and paste the github URL of the project.
3. In source code management, Write your GitHub repository URL
4. Add the SSH Keys in the Github:
Here to integrate both github and Jenkins so that the Jenkins can access the code from github, we have to provide the public keys to github so that it can authorize the access of jenkins.
5. Add credentials for Jenkins:
Select the credentials we have made above and clock on Apply and Save.
6. Build Now:
After filling in all the required options our job is now ready to build.
You can check the status of the build also by simply clicking on the Status.
7. Result of the Job:
To check the result of the job simply click on the Console Output and check whether the job is Successful or Fail.
8. Install some Dependencies:
To run the application using github, jenkins and localhost we need some dependencies to run the application. For this we have to install some dependencies like:
$ sudo apt install nodejs
$ sudo apt install npm
$ sudo npm install
$ node app.js
Here our first requirement is nodejs as the app is made on nodejs so we have to install nodejs in our system, then we have to install npm which is a node package manager and then npm install means that it will install all the packages in package.json file which are required to run the app.
OR you can execute these commands in the Jenkins Job also. To execute commands in the Jenkins Job, go to the job then go to Build Steps and select Execute Shell as we have to execute shell commands here and write the commands there and SAVE the Job.
9. Build the Project:
Click on Build Now and build the job and check the output of the Job.
10. To Verify the Application:
Go to Chrome and search the <Public-IP:8000> and check whether the app is running or not.
Task 2
Step 5: Deployment using Docker
- Install Docker on EC2 instance:
Install docker using the below command
sudo apt-get install docker.io
2. Create a Dockerfile:
To automate things we need a Dockerfile, to create a Docker Image and Container. So we are going to create a Dockerfile here and save it.
$ vim Dockerfile
# Type these steps in the Dockerfile
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node","app.js"]
Add your user to the docker group:
sudo usermod -a -G docker $USER
This command adds your current user to the docker group, which grants permission to access the Docker socket.
3. Create a Docker Image and Docker Container:
By using this Dockerfile we are now going to create a Docker image and Docker Container to run our Application.
To create a Docker image run below command
$ docker build -t to-do-img:v1 .
After creating a Docker image now we are going to create a Docker container from this image, for this run the below command:
$ docker run -d --name to-do-cont -p 8000:8000 to-do-img:v1
OR you can also do this (Docker image and Docker Container) with the help of Jenkins for the automation. For this go to Build Steps and enter the same code in Execute Shell.
After Configuring the Job, click on Apply and Save.
4. Add the Jenkins user to the docker group:
sudo usermod -a -G docker jenkins
This command adds the Jenkins user to the docker group, which grants permission to access the Docker socket.
After creating a User, restart the Jenkins server by:
$ sudo systemctl restart jenkins
5. Build Job:
Click on Build Now and check the output.
Also verify with the help of Console Output to check the output of the Job.
6. Browse public IP address with port no.8000:
Task 3:
The Jenkins pipeline will be triggered automatically by GitHub webhook integration when changes are made to the code repository.
Step 1: Configuring GitHub
1.Go to your GitHub account settings.
And verify, that there is a SSH and GPG Key must be there.
Step 2: For GitHub-Webhook
1.Go to your GitHub repository and click on Settings.
Click on Webhooks and then click on Add Webhook.
In the ‘Payload URL’ field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/. In the ‘Content type’ select: ‘application/json’.
Click on Add Webhook.
Webhook is Added.
Step 3: For Installing GitHub Integration plugin in Jenkins
- Open your jenkins dashboard.
- Click on the Manage Jenkins button on your Jenkins dashboard.
- Click on Manage Plugins.
- Install GitHub Integration plugin.
Step 4: Configuring Jenkins
1.In build Triggers, select ‘Github hook trigger for GITScm polling’.
2. Do some changes in the code
3. Making changes to the file’s content trigger the pipeline and this will automatically build a pipeline.
4. Browse public IP address with port no.8000
You can also visit my Github repository for the complete Code: