Deployment of end-to-end application in node.js using Jenkins CICD, Docker, and AWS EC2 with GitHub Integration.
- Developed a Jenkins Continuous Integration and Continuous Deployment (CICD) Pipeline for a web application, streamlining the deployment process and improving efficiency.
- Implemented automation using webhooks in GitHub, enabling automatic triggers for the CICD pipeline upon code changes, reducing manual intervention, and accelerating the deployment process.
- Gained hands-on experience with Docker, Jenkins, and AWS-EC2, enhancing proficiency in DevOps practices and cloud technologies.
Objective | - Deploy the end-to-end application in node.js using Jenkins CICD with GitHub Integration - Trigger Jenkins pipeline automatically once the code is pushed on GitHub |
Approach | - Using Amazon's Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure - Containerize application by creating Dockerfile - Integrate GitHub with Jenkins using Webhook |
Impact | - Jenkins pipeline triggers automatically once the code is pushed on GitHub - Accomplish faster quality releases by automating CI/CD pipelines |
Primary Technology: Github, Docker, Jenkins, aws EC2 service
Before we write any CI/CD pipeline we need an application to test and deploy. We are going to build a simple to-do application in node.js. Then, create a new repository under your GitHub account.
After logging into your AWS account, search for EC2
Select t2.micro as Instance type and create new key pair to connect to the server
Enter key pair name and select RSA as Key pair type and .pem as Private key file format. Then, click Create key pair
Finally, click Launch instance
Click Connect on the top of the screen
Install Java using following commands:
sudo apt update
sudo apt install openjdk-22-jre
java -version
Install Jenkins using following commands:
**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
Start jenkins using following commands:
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Go to Instances. Click on Security tab Click on the link below Security groups Click on Edit inbound rules Click on Add rule and add port 8080 and select My IP and then click Save rules
On console, type the command sudo cat /var/lib/jenkins/secrets/initialAdminPassword and copy the password
Paste the password in Administrator password to unlock jenkins
Enter Jenkins URL and click Save and Finish
Finally, Jenkins is ready. Click on the button Start using Jenkins
Create new job by clicking New item
Enter an item name. Select Freestyle project. Add description. Select GitHub project and add project url
Select Git as Code Management and add Repository URL. Click on Add to add key
Generate SSH key on console using following commands:
ssh-keygen
cd .ssh
cat id_rsa_pub
Copy the public key
Go to GitHub. Click on Settings
Click on SSH and GPS keys on the left pane and click on Add SSH key
Paste the SSH key and click Add SSH key Go to Jenkins, and select SSH Username with private key in kind On console, enter the command cat id_rsa and copy the private key Paste the private key in jenkins wizard
Select ubuntu(This is for github and jenkins integration) in credentials
Enter */master in Branch Specifier and click Save
In jenkins, click on Build Now on the left pane
Now, click on #1 and select Console Output to view the console
To check whether we got the code on EC2 instance, go to console, and enter the following commands:
sudo cd /var/lib/jenkins/workspace/todo-node-app
ls
Clearly, the code is present in this directory
Click Add rule and enter port number 8000 and select Anywhere IPv4 it can be accessed by anyone. Click Save rules
On console, run the following commands:
sudo apt install nodejs
sudo apt install npm
npm install
node app.js
Remove Dockerfile and install Docker using following command:
sudo rm Dockerfile
sudo apt install docker.io
Edit Dockerfile using the command sudo vim Dockerfile and add following commands within it:
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node", "app.js"]
On console, enter the following commands:
sudo usernod -a -G docker $USER
sudo reboot
To give permission to docker and reboot the system
After restarting, enter the following command:
sudo build . -t todo-node-app
After building docker, enter the following command:
docker run -d --name node-todo-app -p 8000:8000 todo-node-app
To check status of container docker ps
On console, enter the following commands to terminate a container:
docker ps
docker kill
On jenkins, go to dashboard and select the project. For me, it is todo-node-app
Click on configure on the left pane
Click on Build Steps on the left pane and then select Execute shell in the Build Steps section
Enter the following commands in the Execute shell to be executed:
docker build . -t node-app-todo
docker run -d --name node-app-container -p 8000:8000 node-app-todo
Click Save
Click on Build Now on the left pane
On console, enter the following commands to terminate a container:
docker ps
docker kill
On jenkins, go to dashboard and select the project. For me, it is todo-node-app
Click on Manage Jenkins on the left pane and then click on Manage Plugins
Search for GitHub Integration and select GitHub Integration. Finally, click on Download without restart
On aws, go to instances and click on Security pane
Edit inbound traffic of port 8080 to Anywhere IPv4 and click Save rules
Go to GitHub and click Settings
Ensure that SSH and GPG keys are present on your GitHub account
Now, click on repository settings
Click on Webhooks on left pane and click on Add webhook to add a webhook
Add Payload URL and select content type as application/json
Go to Jenkins Job and click on configure on left pane
Click on Build Triggers on left pane and enable **GitHub hook trigger for GITScm polling. Finally, click Save
Go to GitHub project repository and edit the project. Click Commit changes
Clearly, Jenkins pipeline are triggered as the code is pushed on GitHub