Skip to main content
This guide will help you set up a complete Jenkins CI/CD pipeline to automate building, testing, and deploying a Node.js application using Docker.

Prerequisites

Before you begin, ensure you have:
  • AWS EC2 instance running Ubuntu
  • GitHub account
  • Basic understanding of Docker and Jenkins
Make sure to configure security groups on your EC2 instance to allow traffic on ports 22 (SSH), 8080 (Jenkins), and 80 (Application).

Setup steps

1

Clone the repository

Clone the Jenkins pipeline repository to your local machine:
git clone https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git
cd Jenkins-Pipeline-for-CI-CD
The repository contains:
  • Jenkinsfile - Pipeline configuration
  • Dockerfile - Node.js application containerization
  • index.js - Simple Express application
  • package.json - Node.js dependencies
2

Install Jenkins on EC2

SSH into your EC2 instance and install Jenkins:
sudo apt update
sudo apt upgrade -y
Access Jenkins at http://<your-ec2-public-ip>:8080 and complete the initial setup wizard.
3

Configure Docker permissions

Install Docker and grant Jenkins user permission to use it:
Install Docker
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
Configure permissions
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
The Jenkins service must be restarted after adding the jenkins user to the docker group. This is critical for the pipeline to work.
4

Install Jenkins plugins

In the Jenkins dashboard, navigate to Manage Jenkins > Manage Plugins and install:
  • Docker plugin
  • Pipeline plugin
  • GitHub Integration plugin
Restart Jenkins after installing plugins.
5

Create Jenkins pipeline job

  1. Click New Item in Jenkins dashboard
  2. Enter a name (e.g., “nodejs-demo-app”)
  3. Select Pipeline and click OK
  4. In the Pipeline section, select Pipeline script from SCM
  5. Choose Git as SCM
  6. Enter repository URL: https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git
  7. Set branch to */main
  8. Save the configuration
6

Configure GitHub webhook

Set up automatic builds when you push code to GitHub:
  1. Go to your GitHub repository settings
  2. Navigate to Webhooks > Add webhook
  3. Set Payload URL: http://<your-ec2-public-ip>:8080/github-webhook/
  4. Content type: application/json
  5. Select Just the push event
  6. Click Add webhook
The webhook triggers the Jenkins pipeline automatically whenever you push commits to the repository.
7

Trigger your first build

You can trigger a build in two ways:Manual trigger:
  • Click Build Now in your Jenkins job
Automatic trigger:
  • Make a change to your repository
  • Commit and push to GitHub
  • Jenkins will automatically start the pipeline
The pipeline will execute four stages:
  1. Clone - Fetch code from GitHub
  2. Build - Create Docker image
  3. Test - Run npm tests
  4. Deploy - Launch Docker container
8

Verify deployment

Once the pipeline completes successfully:
  1. Check the Jenkins console output for “Pipeline completed!”
  2. Verify the Docker container is running:
    docker ps | grep nodejs-demo-app
    
  3. Access your application at http://<your-ec2-public-ip>
You should see the message: “Hello, Elevate Labs, This is After pushing code commits to GitHub”

Pipeline stages explained

The Jenkins pipeline defined in Jenkinsfile includes:
stage('Clone') {
    steps {
        echo 'Cloning the repository...'
        git branch: 'main', url: 'https://github.com/mani-6666/Jenkins-Pipeline-for-CI-CD.git'
    }
}

Troubleshooting

If you see “permission denied while trying to connect to Docker daemon”:
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
Wait a few seconds for Jenkins to restart, then retry the build.
If port 80 or 3000 is already in use:
# Find and stop the container using the port
docker ps
docker stop <container-name>
docker rm <container-name>
Verify webhook configuration:
  • Ensure Payload URL ends with /github-webhook/
  • Check security group allows inbound traffic on port 8080
  • Review webhook delivery history in GitHub settings

Next steps

Setup Guide

Detailed setup instructions for each component

Pipeline Configuration

Learn about pipeline stages and customization

Troubleshooting

Common issues and solutions

AWS EC2 Setup

Complete EC2 instance configuration guide

Build docs developers (and LLMs) love