Node.js CI/CD

Here’s a comprehensive guide on Node.js CI/CD (Continuous Integration & Continuous Deployment), covering pipelines, tools, best practices, and examples.


πŸš€ Node.js CI/CD – Complete Guide

CI/CD automates building, testing, and deploying Node.js applications to production or staging environments, reducing errors and improving speed.

  • CI (Continuous Integration) β†’ Automates testing & building on code commits.

  • CD (Continuous Deployment / Delivery) β†’ Automates deployment to servers.


1️⃣ Why CI/CD for Node.js?

  • Detect bugs early (unit tests, integration tests)

  • Ensure consistent builds across environments

  • Automated deployment to staging/production

  • Reduce human errors

  • Faster development & delivery


2️⃣ Key Steps in Node.js CI/CD Pipeline

  1. Code Commit β†’ Push code to GitHub, GitLab, Bitbucket, etc.

  2. Install Dependencies β†’ npm install or yarn install

  3. Linting / Static Analysis β†’ eslint, prettier

  4. Unit / Integration Testing β†’ jest, mocha, supertest

  5. Build / Bundle β†’ webpack, babel, tsc (TypeScript)

  6. Dockerize Application (optional) β†’ Dockerfile

  7. Deployment β†’ Server, cloud, or container orchestration

  8. Monitoring & Logging β†’ PM2, Sentry, New Relic


3️⃣ Popular CI/CD Tools

ToolTypeFeatures
GitHub ActionsCI/CDIntegrated with GitHub, free tiers, workflow files
GitLab CI/CDCI/CDYAML pipelines, Docker support
CircleCICI/CDEasy to configure, cloud/VM options
JenkinsCI/CDHighly customizable, plugin ecosystem
Travis CICI/CDSimple GitHub integration
Bitbucket PipelinesCI/CDCloud CI/CD integrated with Bitbucket

4️⃣ Example: GitHub Actions Workflow for Node.js

.github/workflows/nodejs.yml

name: Node.js CI/CD

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build-test-deploy:
runs-on: ubuntu-latest

steps:
# Checkout code
- uses: actions/checkout@v3

# Setup Node.js
- uses: actions/setup-node@v3
with:
node-version: 20

# Install dependencies
- run: npm install

# Lint code
- run: npm run lint

# Run tests
- run: npm test

# Build (if needed)
- run: npm run build

# Deploy (example using rsync to server)
- name: Deploy to Server
uses: appleboy/ssh-action@v0.1.7
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
source: "."
target: "/var/www/myapp"

βœ… This workflow:

  • Installs Node.js

  • Runs lint & tests

  • Builds the project

  • Deploys to a remote server


5️⃣ Example: GitLab CI/CD for Node.js

.gitlab-ci.yml

stages:
- install
- test
- build
- deploy

install_dependencies:
stage: install
image: node:20
script:
- npm install

test:
stage: test
script:
- npm run lint
- npm test

build:
stage: build
script:
- npm run build

deploy:
stage: deploy
script:
- scp -r . user@server:/var/www/myapp
only:
- main


6️⃣ Docker + CI/CD

Docker simplifies deployment:

Dockerfile

FROM node:20

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "app.js"]

CI/CD Pipeline Example

  1. Build Docker image

docker build -t myapp:latest .
  1. Push to registry

docker tag myapp registry.example.com/myapp:latest
docker push registry.example.com/myapp:latest
  1. Deploy container on server or Kubernetes


7️⃣ Environment Variables in CI/CD

  • Use secrets in CI/CD platform (GitHub Secrets, GitLab Variables)

  • Never commit .env to repo

  • Example (GitHub Actions):

env:
NODE_ENV: production
DB_HOST: ${{ secrets.DB_HOST }}

8️⃣ Monitoring & Rollback

  • PM2 β†’ Process manager for Node.js, monitoring, zero-downtime reload

pm2 start app.js --name myapp
pm2 reload myapp
  • Sentry / New Relic β†’ Monitor errors and performance

  • Rollback strategies:

    • Keep previous Docker image or server folder

    • Use Git tags for quick revert


9️⃣ Best Practices

  1. Run linting, tests, and builds in CI before deployment

  2. Keep CI/CD scripts fast and minimal

  3. Use secrets management for sensitive data

  4. Use staging environment before production

  5. Monitor application after deployment


🎯 Summary

  • CI/CD automates testing and deployment of Node.js apps

  • Node.js pipelines typically include: install β†’ test β†’ build β†’ deploy

  • Tools: GitHub Actions, GitLab CI, Jenkins, CircleCI

  • Docker + CI/CD simplifies environment consistency

  • Use secrets for environment variables

  • Monitor performance and errors in production

You may also like...