Hello
This is my project Article on Devops where I am going to build a project on Jenkins.
I will be deploying a Node js project using AWS, Docker, and Jenkins.
Role of DevOps Engineer:
Usually, when a developer develops his code he will push his code to GitHub.
As a DevOps Engineer, You need to take the code from GitHub and test it in different environments (any cloud it can be) if the code is running without any issues and finally deliver it to the AWS instance using Jenkins.
Docker: If the Developer writes code in any of the platforms whether it is Linux, Windows, etc, Docker will provide a virtualized environment to run the code despite any platform.
Tools required for this project:
Docker
GitHub
AWS
Jenkins
We are using the node.js application. We need to build a pipeline where this code will run. We will be deploying our application in the AWS server, Docker and Jenkins.
We also you webhook where Jenkins automatically builds the project when there is a change in the application code.
Deploying in AWS Server:
Steps:
Create an AWS EC2 instance and connect it to the server.
Installations needed in AWS server:
-->Install Jenkins Refer --> install-jenkins-unbuntu (trainwithshubham.com)
-->Open the 8080 port to setup Jenkins and start working with it
Note: Add TCP 8080 rule in the security groups of the AWS Instance
Now Connect to Jenkins. Type the public address of your instance:8080(it's a default port num of Jenkins)
Ex: 13.233.64.80:8080
It asks for the password. To know the password
Give sudo cat /var/lib/jenkins/secrets/initialAdminPassword in the server and paste the pwd. Install all the plugins. Now jenkins is ready.
Now create a job in freestyle project(A freestyle project in Jenkins is a project that spans multiple operations. It can be a build, a script run, or even a pipeline.)
Add description, select github project and enter githul repo URL where project is there.
Integrating Jenkins and GitHub:
Now to connect your server with GitHub you need SSH(Secure shell)
server ------->GitHub
It has a private key and a public key
Steps:
Type ssh-keygen
It will generate private and public keys and store in .ssh
The private key is stored in id rsa
The public key is stored in id rsa pub
Add this public key in SSH keys of github
Now GitHub has public access to connect with my server
Integrate GitHub with Jenkins
Select git, add a private key to the Jenkins
goto credentials and select SSH username with private key and give your private key from server here, give username ubuntu, choose radio button and save the changes. Build the project.
Jenkins will create a workspace and Code will be present in that workspace.
Follow the readme to run the app and install the necessary software mentioned
If we give ctrl + C the app will stop.
We need to implement Docker now:
We want the app to run anywhere.
Docker: It is a containerization platform
Container: Which is having applications, and dependencies. which can be easily shipped to run on other machines. (Dependencies: Ex: If you want to run selenium, selenium becomes its dependency)
Image: Image is an executable package of software that includes everything needed to run an application
We need to make an image from the Dockerfile. Dockerfile is a text file where it has all the steps needed to run. We will create an image from the docker file which contains a set of instructions ( OS, software needed to run the app, running server, setting up ports, etc -->all the steps we performed above as a file)
By using this image, we can run the container automatically app will run despite of any platform.
Steps:
Install docker : sudo apt install docker.io
goto the path where app is present
cd /var/lib/jenkins/workspace/Node-Todo
Note: If you want to remove docker file and add a new one then you can remove it by
rm Dockerfile
Create a Dockerfile in the app path as below.
vi Dockerfile (D should be uppercase)
add below commands
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node","app.js"]
FROM node:12.2.0-alpine --> This will create an OS with node installed ( node:12.2.0-alpine-->it is a node image)
WORKDIR app -->app named folder will be created as a working directory
COPY . . -->copy code from the server to the container/source to dest
RUN npm install -->it will install npm
EXPOSE 8000 -->it will open 8000 where the app will run
CMD ["node","app.js"] -->its a command to run the server
Now build image from dockerfile
sudo docker build . -t todo-node-app
Now run the docker image. It will create a container
sudo docker run -d --name nodetodoapp -p 8000:8000 todo-node-app
and check if app is running or not (public IP :8000)
Kill the container
Running in Jenkins
Got to jenkins job -->Execute shell
docker build . -t node-app-todo
docker run -d --name node-app-container -p 8000:8000 node-app-todo
if you are facing permission issues, remove sudo and in the server give
sudo usermod -a -G docker jenkins and
restart jenkins-->sudo systemctl restart jenkins
We successfully deployed into jenkins also
Kill the container
Concept of Webhook:
Whenever changes happen in GitHub, it triggers Jenkins.
Whenever there is a change in the code Jenkins should automatically build the app without the person clicking on build now.
Steps:
Install a plugin called GitHub integration
Goto repository setting -->webhook-->add webhook-->payload URL (http://3.110.172.33:8080/github-webhook/) and select as below
It should be marked as a tick
Now to go to your job -->configure-->build triggers-->select GitHub hook trigger as below.
Working:
Modify the code and let's see :)
You can see the build is started.
So far we ran the application by using an AWS server, Docker, Jenkins, and used the concept of webhook
Thank you for reading! Happy Learning :)