Skip to main content

Command Palette

Search for a command to run...

Complete DevOps CI/CD Project

Deploying Nodejs Application

Updated
Complete DevOps CI/CD Project
H

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:

  1. AWS Account: AWS account is required for making ec2 instances on which our project will run.

  2. Jenkins Installed: Jenkins should be installed and running on your machine or server. You can download it from here.

  3. Docker Installed: Ensure Docker is installed on the Jenkins server. Follow the official Docker documentation to install Docker.

  4. Node.js App on GitHub: Clone this repository for the code in your GitHub https://github.com/harshitsahu2311/Nodejs-Project.git.

  5. 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:

  1. 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.

  1. Connect to the EC2 Instance:

    • SSH into your EC2 instance using the key pair.
    ssh -i "your-key.pem" ec2-user@your-ec2-ip
  1. 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
    

  2. Install Docker:

     sudo yum install docker.io -y
     sudo systemctl start docker
     sudo usermod -aG docker jenkins
    
  3. Access 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/initialAdminPassword file.

Step 2: Install Jenkins Plugins

After setting up Jenkins, install the necessary plugins to integrate Jenkins with GitHub and Docker.

  1. 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.

  1. 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.

  1. Configure GitHub Repository:

Step 4: Add your GitHub credentials

  1. For this first, go to EC2 Instance and run this command

     sudo ssh-keygen
    
  2. Then copy the public key and paste it into GitHub -> Settings -> SSH keys -> create new -> name: node-project -> key: paste the Public Key.

  3. 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

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

  5. Build Steps: Execute Shell

     docker build -t node-project .
     docker run -d --name node-container -p 3000:3000 node-project
    

  6. 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.

  1. Add Jenkins to the Docker Group:

     sudo usermod -aG docker jenkins
     sudo systemctl restart jenkins
    
  2. Test 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).

  1. Go to EC2 Dashboard > Security Groups.

  2. Edit the inbound rules to allow traffic on port 8080 (for Jenkins) and port 3000 (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

  1. Go to your Jenkins dashboard and select the nodejs-project job.

  2. Click Build Now to start the pipeline.

  3. 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 🚀

F

For Jenkins installation, as a prerequisite java - 11 version is not supportive better prefer a higher version like 17 as recommended in Jenkins installation document

DevOps Projects

Part 2 of 18

A curated series of practical DevOps projects showcasing my journey from foundational concepts to advanced workflows, including CI/CD, container orchestration, IaC, and cloud solutions—highlighting real-world expertise and continuous learning.

Up next

Project: AWS DevOps Deployment

Streamlining Infrastructure with CI/CD and Automation