🚀 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:
- Download Jenkins from jenkins.io
- Run the installer
- Access Jenkins at http://localhost:8080
- Unlock Jenkins using the initial password shown in the terminal
- Install recommended plugins
- Create your first admin user
- 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
Recommended by LinkedIn
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)
- Download from ngrok.com
- Install and connect to your account:
ngrok config add-authtoken <your_token>
- Expose Jenkins (default port is 8080):
ngrok http 8080
- Copy the HTTPS forwarding URL (e.g., https://abc123.ngrok.io)
🔸 GitHub Webhook Configuration
- Go to your GitHub repo → Settings → Webhooks
- Click Add Webhook:
🔸 Jenkins Configuration
- Go to your pipeline job → Configure
- 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)
- Go to Manage Jenkins → Configure System
- Scroll to GitHub → GitHub Servers → Add GitHub Server
- Set Name: GitHub
- Leave API URL default
- Click Add Credentials → Kind: Secret Text
🔐 How to Generate GitHub Personal Access Token
- Go to https://github.com → Top-right profile → Settings
- Developer settings → Personal Access Tokens → Tokens (classic)
- Click Generate new token
- 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
- In Jenkins, go to New Item → Pipeline
- Under Pipeline → Definition, choose Pipeline from SCM
- Set:
- Save and click Build Now
✅ From now on, any git push will automatically trigger the pipeline!
🧪 Validate Docker Image
Check Docker Hub:
- Visit your repository to confirm image push
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!