Complete DevOps CI/CD Project
Deploying Nodejs Application

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).
Continuous Integration and Continuous Deployment (CI/CD) pipelines have revolutionized how modern applications are built and deployed. Automating this process helps reduce errors, improve efficiency, and ensure consistency across various environments. In this blog, we'll walk through the steps to deploy a Node.js application from GitHub to Docker using Jenkins, one of the most popular CI/CD tools.
Prerequisites
Before we begin, make sure you have the following prerequisites:
AWS Account: AWS account is required for making ec2 instances on which our project will run.
Jenkins Installed: Jenkins should be installed and running on your machine or server. You can download it from here.
Docker Installed: Ensure Docker is installed on the Jenkins server. Follow the official Docker documentation to install Docker.
Node.js App on GitHub: Clone this repository for the code in your GitHub https://github.com/harshitsahu2311/Nodejs-Project.git.
Jenkins Docker Plugin: The Docker plugin for Jenkins must be installed. This allows Jenkins to interact with Docker.
Step 1: Set Up Jenkins on AWS EC2
First, let's configure Jenkins on the AWS EC2 instance. If Jenkins has not already been installed, you can set it up using the following steps.
Installing Jenkins on AWS EC2:
Launch EC2 Instance:
Choose an Amazon Linux 2 AMI or Ubuntu.
Make sure to allow inbound traffic on port 8080 for Jenkins in the security group.

Connect to the EC2 Instance:
- SSH into your EC2 instance using the key pair.
ssh -i "your-key.pem" ec2-user@your-ec2-ip
Install Jenkins:
For Amazon Linux 2:
sudo yum update -y sudo amazon-linux-extras install java-openjdk11 wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key sudo yum install jenkins -y sudo systemctl start jenkins sudo systemctl enable jenkins
Install Docker:
sudo yum install docker.io -y sudo systemctl start docker sudo usermod -aG docker jenkinsAccess Jenkins:
In a browser, go to
http://your-ec2-ip:8080.Follow the steps to unlock Jenkins using the initial password from the
/var/lib/jenkins/secrets/initialAdminPasswordfile.
Step 2: Install Jenkins Plugins
After setting up Jenkins, install the necessary plugins to integrate Jenkins with GitHub and Docker.
Install GitHub Integration Plugins:
Go to Manage Jenkins > Manage Plugins.
Install the following plugins:
- Git Integration Plugin (for pushing the repository from GitHub through Webhook).

Step 3: Create a Jenkins FreeStyle Job
To deploy the Node.js app, create a new free-style job in Jenkins.
Create Free Style Job:
From the Jenkins dashboard, click New Job, then select Free Style.
Name your job (e.g., nodejs-project ) and click OK.

Configure GitHub Repository:
In General click on GitHub Project and paste URL https://github.com/harshitsahu2311/Nodejs-Project.git.
Choose Git as the SCM and enter your GitHub repository URL.
Step 4: Add your GitHub credentials
For this first, go to EC2 Instance and run this command
sudo ssh-keygenThen copy the public key and paste it into GitHub -> Settings -> SSH keys -> create new -> name: node-project -> key: paste the Public Key.

Then again go to EC2 and copy the Private key and then come to Jenkins -> SCM -> git -> add credentials -> paste the key in key column


Build Triggers -> Click on this option "GitHub hook trigger for GITScm polling"

Build Steps: Execute Shell
docker build -t node-project . docker run -d --name node-container -p 3000:3000 node-project
Apply and Save
Step 5: Configure Docker on EC2
Ensure Docker is running properly on the EC2 instance and Jenkins has permission to execute Docker commands.
Add Jenkins to the Docker Group:
sudo usermod -aG docker jenkins sudo systemctl restart jenkinsTest Docker Access:
- SSH into your EC2 instance and run a simple Docker command to ensure Docker is working:
docker run hello-world
Step 6: Set Up AWS Security Groups
Ensure your AWS security group allows traffic for Jenkins (port 8080) and your Node.js app (port 3000, or whichever port the app uses).
Go to EC2 Dashboard > Security Groups.
Edit the inbound rules to allow traffic on port
8080(for Jenkins) and port3000(for the Node.js app).

Step 7: Run the code on the EC2 for to check
Run these commands:
sudo apt install nodejs
sudo apt install npm
npm install
node app.js

You should find this -

Step 8: Create a Webhook for Automation
Go to Repository -> Settings -> Webhooks -> create a webhook by the URL of Jenkins

Step 9: Run the Jenkins Pipeline
Go to your Jenkins dashboard and select the nodejs-project job.
Click Build Now to start the pipeline.
Jenkins will:
Clone the Node.js app from GitHub.
Build a Docker image.
Push the Docker image to Docker Hub.
Deploy the containerized Node.js app on your AWS EC2 instance.
But I know you are not satisfied with this; you want a fully automated project for that do one thing make any changes in the .js file and see the magic of webhooks in Jenkins.
The Jenkins will Build it by itself when any changes made in the code and refresh the URL.
Step 10: Access the Deployed Application
Once the pipeline completes, your Node.js app should be running in a Docker container on your EC2 instance. You can access the application by going to:
http://your-ec2-ip:3000

Conclusion
By combining Jenkins, Docker, and AWS, you’ve created a powerful and scalable CI/CD pipeline for your Node.js app. This setup allows you to automatically build, test, and deploy your application in isolated Docker containers, ensuring consistency across environments. You can extend this setup by adding further stages like automated tests or integrating with other AWS services.
Automating your deployment with Jenkins on AWS is a highly efficient way to manage modern applications, enabling smooth development workflows and fast, reliable deployments.
HAPPY DEPLOYING 🚀





