Deployment of todo-app through jenkins on AWS EC2

Harshit SahuHarshit Sahu
4 min read

In this project, I have successfully deployed the Todo-app which is available on my GitHub whose URL I have provided below: https://github.com/harshitsahu2311/Django-todo-automate

Firstly, you have to check that is this app is running in your local environment or not. For that, I have uploaded an individual blog on how to run an app in the local environment, I have provided the link below you can check out: https://hashnode.com/edit/clyvjiq8q000209l5gp1why6r

Firstly create an instance(UBUNTU) in AWS with free-tier eligible and access it through SSH in your CLI and then follow the steps to run this app.

I have run this system on my virtual box so I will access it from there only but don't worry steps are the same after accessing the Linux

Create a folder named "project" in your instance and then open it

mkdir project
cd /project

Get the code in that instance using this instance:

git clone https://github.com/harshitsahu2311/Django-todo-automate

DOCKER INSTALLATION

Then to automate we have to install the docker

sudo apt install docker.io
sudo apt update

then check also that docker is installed or not by this command

docker -v

After that create Dockerfile to install all the dependencies in one click

vi Dockerfile
FROM python:3
RUN pip install django==3.2

COPY . .
RUN python manage.py migrate

CMD ["python","manage.py","runserver","0.0.0.0:8001"]

to check whether this Dockerfile is correct or not you can run it manually also by -

sudo docker build -t todo-app .
sudo docker images
sudo docker run -p 8001:8001 todo-app

Now you can access it with your public_IP:8001

After that turn off the container also

sudo docker kill todo-app

SETUP OF GITHUB

After that Go to GitHub account -> Settings -> Developer Settings -> Personal Access Token -> Generate a new token -> name: jenkins-cicd -> GENERATE TOKEN

COPY the URL and paste it in your note pad

Then create a public repository with name Todo-App and then copy the URL of repo and then again come to aws-machine and follow these steps:

git remote -v

Now here you have to connect git to your repository in order to send this data in your repo for that run this command:-

git remote set-url origin <repository-url>
git remote -v
git status
git add .
git commit -m "Added Server code"
git push origin main

After that enter your GitHub username and in password enter the URL of personal token which you have copied that time which is your unique identity of GitHub and through this repository is successfully added to you repository. It will look like this

INSTALLATION OF JENKINS

After that install Jenkins in your instance, you can also read my blog on how to install Jenkins on AWS by this link: https://harshitsahu2311.hashnode.dev/install-jenkins-on-aws

sudo apt update
sudo apt install openjdk-11-jre
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.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
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

Copy the public IP from the AWS console and Open PUBLIC_IP:8080 :-

you will get copy the path and run

cat /var/jenkins_home/secrets/intialAdminPassword

After that enter the password on this page and install suggested plugins and then create an account in it after you are successfully login to your Jenkins home page

CREATE AN AGENT IN JENKINS

Now click on the set up an agent -> Node Name: Todo-app-dev -> sumbit

After a new page will open there fill these details:-

Description: This node is for Todo App Pipeline

Number of executors: 1

Remote root directory: /home/ubuntu

Labels: todo-dev

Usage: Use this node as much as possible -> SAVE

CREATE A JOB IN JENKINS

Now click on CREATE A JOB -> Enter name : todo-app-cicd in free style projects -> Click OK

Here in Source Code Management -> click on Git -> In Repository URL paste the URL of your repository -> In credentials click on add -> on kind option select secret text -> In secret paste the link of the personal access token of GitHub -> ID: jenkins-github-cicd

Click on the option Test connection on right hand side below this you will it will show your GitHub username.

Branch : */main

Build Steps -> Add build Steps:-

sudo docker build . -t todo-app

sudo docker run -p 8001:8001 -d todo-app

SAVE

Please Ensure no service is running in port 8001 to check run command on CLI

lsof -i:8001

Grant the permission to django-todo to be access by anyone by-

chmod 777 /django-todo

After that Click on Build Now -> It will show a green tick

Now click on browser and enter PUBLIC-IP:8001

Congratulations!! you have successfully deployed your Todo-application in server using Jenkins.

OUTCOME

In this project we have learned how to push the code in GitHub and then create a pipeline through which code is being fetch from GitHub and then run on docker container using Dockerfile and successfully accessed it using Public IP of AWS

0
Subscribe to my newsletter

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

Written by

Harshit Sahu
Harshit Sahu

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).