🚀 CI/CD Pipeline for Node.js with Jenkins & Docker (Windows + Linux)

🚀 CI/CD Pipeline for Node.js with Jenkins & Docker (Windows + Linux)


In this deeply detailed tutorial, we'll build a complete CI/CD pipeline using Jenkins to automate the following:

  • Create a simple Node.js app
  • Write a Dockerfile to containerize the app
  • Install and configure Jenkins on Windows and Linux
  • Write a Jenkinsfile to automate testing and Docker deployment
  • Use Jenkins secrets to store Docker credentials
  • Trigger the pipeline automatically on git push using GitHub Webhook


🧱 Step 1: Create Your Node.js Application

1. Create the project directory

mkdir ci-cd-node-app-jenkins
cd ci-cd-node-app-jenkins        

2. Initialize Node.js project

npm init -y        

This creates a package.json file with default values.

3. Create a simple app

Create index.js:

const sum = require('./sum');

console.log("App started. Sum of 2 and 3 is:", sum(2, 3));        

Create sum.js:

function sum(a, b) {
  return a + b;
}

module.exports = sum;        

4. Add a test using Jest

Install Jest:

npm install --save-dev jest        

Update package.json:

"scripts": {
  "test": "jest"
}        

Create sum.test.js:

const sum = require('./sum');

test('adds 2 + 3 to equal 5', () => {
  expect(sum(2, 3)).toBe(5);
});        

5. Add .gitignore

node_modules
.env        


🐳 Step 2: Add Dockerfile

Create Dockerfile:

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]        

To test locally:

docker build -t yourusername/jenkins-node-app .
docker run yourusername/jenkins-node-app        


🔧 Step 3: Install Jenkins

🔹 On Windows:

  1. Download Jenkins from jenkins.io
  2. Run the installer
  3. Access Jenkins at http://localhost:8080
  4. Unlock Jenkins using the initial password shown in the terminal
  5. Install recommended plugins
  6. Create your first admin user
  7. Go to Manage Jenkins → Global Tool Configuration

👉 Also install Docker Desktop and verify it's working using docker --version

🐧 On Linux (Ubuntu):

sudo apt update && sudo apt install -y openjdk-17-jdk git docker.io
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.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 update && sudo apt install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins        

Then:

sudo usermod -aG docker jenkins
sudo systemctl restart jenkins        

Visit http://localhost:8080 and follow the setup wizard.


🔑 Step 4: Add DockerHub Secrets to Jenkins

Navigate to:

  • Manage Jenkins → Credentials → Global → Add Credentials

Add two "Secret text" entries:

  • ID: docker-username, Secret: Your Docker Hub username
  • ID: docker-password, Secret: Your Docker Hub password

These are used in the Jenkinsfile as:

environment {
  DOCKERHUB_USERNAME = credentials('docker-username')
  DOCKERHUB_PASSWORD = credentials('docker-password')
}        


🌐 Step 5: Connect GitHub Webhook to Jenkins (Auto Trigger Pipeline)

🔸 Set Up Ngrok (For local Jenkins)

  1. Download from ngrok.com
  2. Install and connect to your account:

ngrok config add-authtoken <your_token>        

  1. Expose Jenkins (default port is 8080):

ngrok http 8080        

  1. Copy the HTTPS forwarding URL (e.g., https://abc123.ngrok.io)

🔸 GitHub Webhook Configuration

  1. Go to your GitHub repo → Settings → Webhooks
  2. Click Add Webhook:

🔸 Jenkins Configuration

  1. Go to your pipeline job → Configure
  2. Under Build Triggers, check:

✅ Result

Whenever you git push, GitHub will trigger Jenkins to build automatically.

💡 If webhook fails, you can fallback to:


🔐 Step 6: Add GitHub Server in Jenkins (Optional but Recommended)

  1. Go to Manage Jenkins → Configure System
  2. Scroll to GitHub → GitHub Servers → Add GitHub Server
  3. Set Name: GitHub
  4. Leave API URL default
  5. Click Add Credentials → Kind: Secret Text

🔐 How to Generate GitHub Personal Access Token

  1. Go to https://github.com → Top-right profile → Settings
  2. Developer settings → Personal Access Tokens → Tokens (classic)
  3. Click Generate new token
  4. Copy and use this token in Jenkins


📂 Step 7: Create Jenkinsfile

▶ Jenkinsfile for Windows

pipeline {
  agent any

  tools {
    nodejs "nodejs20"
  }

  environment {
    DOCKERHUB_USERNAME = credentials('docker-username')
    DOCKERHUB_PASSWORD = credentials('docker-password')
  }

  stages {
    stage('Checkout Code') {
      steps {
        git 'https://github.com/TejeshKumarGantyada/4-cicd-pipeline-with-jenkins.git'
      }
    }

    stage('Install Dependencies') {
      steps {
        bat 'npm install'
      }
    }

    stage('Run Tests') {
      steps {
        bat 'npm test'
      }
    }

    stage('Docker Build') {
      steps {
        bat "docker build -t %DOCKERHUB_USERNAME%/jenkins-node-app:latest ."
      }
    }

    stage('Docker Login') {
      steps {
        bat "echo %DOCKERHUB_PASSWORD% | docker login -u %DOCKERHUB_USERNAME% --password-stdin"
      }
    }

    stage('Docker Push') {
      steps {
        bat "docker push %DOCKERHUB_USERNAME%/jenkins-node-app:latest"
      }
    }
  }
}        

▶ Jenkinsfile for Linux

pipeline {
  agent any

  tools {
    nodejs "nodejs20"
  }

  environment {
    DOCKERHUB_USERNAME = credentials('docker-username')
    DOCKERHUB_PASSWORD = credentials('docker-password')
  }

  stages {
    stage('Checkout Code') {
      steps {
        git 'https://github.com/TejeshKumarGantyada/4-cicd-pipeline-with-jenkins.git'
      }
    }

    stage('Install Dependencies') {
      steps {
        sh 'npm install'
      }
    }

    stage('Run Tests') {
      steps {
        sh 'npm test'
      }
    }

    stage('Docker Build') {
      steps {
        sh "docker build -t $DOCKERHUB_USERNAME/jenkins-node-app:latest ."
      }
    }

    stage('Docker Login') {
      steps {
        sh "echo $DOCKERHUB_PASSWORD | docker login -u $DOCKERHUB_USERNAME --password-stdin"
      }
    }

    stage('Docker Push') {
      steps {
        sh "docker push $DOCKERHUB_USERNAME/jenkins-node-app:latest"
      }
    }
  }
}        


✅ Step 8: Run the Pipeline

  1. In Jenkins, go to New Item → Pipeline
  2. Under Pipeline → Definition, choose Pipeline from SCM
  3. Set:
  4. Save and click Build Now

✅ From now on, any git push will automatically trigger the pipeline!

Article content


🧪 Validate Docker Image

Check Docker Hub:

  • Visit your repository to confirm image push

Article content

Test image locally:

docker pull yourusername/jenkins-node-app
docker run yourusername/jenkins-node-app        


This is 🔥🔥🔥 You basically built the DevOps starter kit every Node.js app dreams of! Loved the attention to detail, ngrok for webhooks, secure credentials, full automation… Next up: real-time observability? 😉 (You’ve already got the perfect pipeline for it.) Huge props, this is how you level up for real!

To view or add a comment, sign in

More articles by Tejesh Kumar Gantyada

Others also viewed

Explore content categories