How would you design a CI/CD pipeline for a Node.js microservice?
AdvancedProductionNode.js builds (npm, package.json)
Answer
Short answer
On every PR: install with npm ci (cached), lint, unit test with coverage, scan dependencies and secrets. On merge to main: build the Docker image (multi-stage), scan it, push with an immutable tag (version + Git SHA), deploy to staging, run smoke tests, then promote the same image to production (often via GitOps).
Example (GitHub Actions)
name: shop-api
on: { pull_request: {}, push: { branches: [main] } }
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version-file: .nvmrc, cache: npm }
- run: npm ci
- run: npm run lint
- run: npm test -- --coverage
- run: npm audit --audit-level=high
- uses: gitleaks/gitleaks-action@v2
image:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build -t $REGISTRY/shop-api:${{ github.sha }} .
- run: trivy image --exit-code 1 --severity CRITICAL $REGISTRY/shop-api:${{ github.sha }}
- run: docker push $REGISTRY/shop-api:${{ github.sha }}
# then: update the image tag in the GitOps repo → Argo CD deploys to staging
Key design points
- Node version from
.nvmrc— CI matches developers. - Cache npm downloads keyed on the lockfile.
- Immutable tags — never deploy
latest. - Graceful shutdown + readiness probe so rolling updates drop no requests.
- Build once, promote the same image digest to prod after staging checks.
What to say in the interview
"PRs get npm ci, lint, tests, audit and secret scanning. Main builds a multi-stage image tagged with the commit SHA, scans it with Trivy, pushes it, and updates the GitOps repo so Argo CD deploys to staging; production gets the exact same image after smoke tests and approval."
More on Node.js builds (npm, package.json)
What is package.json and what does it contain?BeginnerHow do you build and run a Node.js application?BeginnerWhat is the difference between npm install and npm ci?BeginnerWhy must package-lock.json be committed to Git?BeginnerWhat is the difference between dependencies and devDependencies?BeginnerWhat do the ^ and ~ symbols mean in package.json versions?Beginner