Currently viewing the AI version
Switch to human version

Lerna CI/CD Production Deployment - AI-Optimized Knowledge Base

Critical Configuration Requirements

Authentication Setup (BREAKING: Wrong config causes 4+ hour debugging sessions)

Failure Mode: npm ERR! 401 Unauthorized - Silent failure with deprecated auth methods
Root Cause: Lerna moved from npm_config__auth to _authToken without documentation updates

Working Configuration:

env:
  NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
  GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: "Configure npm"
  run: |
    npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
    npm config set registry https://registry.npmjs.org/

Critical: Use _authToken NOT npm_config__auth (deprecated, causes phantom failures)

Git Configuration (CRITICAL: CI fails with cryptic git errors)

Failure Mode: Error: Command failed with exit code 128: git diff --name-only
Root Cause: GitHub Actions shallow clone by default, Lerna needs full history

Required Fix:

- uses: actions/checkout@v3
  with:
    fetch-depth: 0  # Required for Lerna change detection

Docker Build Optimization

Performance Impact

  • Problem: Change one comment, rebuild everything (15+ minute builds)
  • Solution: Multi-stage builds with dependency caching
  • Time Savings: From 15+ minutes to 2-5 minutes

Optimized Dockerfile Structure:

# Stage 1: Cache dependencies separately
FROM node:18-alpine AS deps
WORKDIR /app
COPY package*.json lerna.json ./
COPY packages/*/package*.json ./packages/
RUN npm ci --only=production

# Stage 2: Build specific package only
FROM deps AS build
COPY packages/api ./packages/api
RUN npx lerna run build --scope=@company/api

Critical: Copy dependencies before source code to leverage Docker layer caching

Package Publishing Order and Dependency Management

High-Severity Failure Scenario

Impact: 30-60 seconds of broken installs, production service degradation
Cause: Publishing packages before their dependencies are available

Publishing Sequence Failure:

  1. Update @company/utils from 1.2.0 to 1.3.0
  2. Update @company/ui to depend on @company/utils@^1.3.0
  3. Publish @company/ui@2.1.0 first (CRITICAL ERROR)
  4. Users install UI 2.1.0, get dependency error for utils 1.3.0
  5. Production alerts at 11pm

Automated Solution:

{
  "version": "independent",
  "npmClient": "npm",
  "command": {
    "publish": {
      "conventionalCommits": true,
      "message": "chore(release): publish",
      "registry": "https://registry.npmjs.org"
    }
  }
}

Key: Lerna automatically calculates dependency graph and publishes in correct order

Resource Requirements and Scaling Thresholds

Nx Cloud Cost Analysis

Break-even Point: Teams with 10+ developers, builds exceeding 15+ minutes
Cost Range: $500+/month for large monorepos after free tier
Alternative: Local caching sufficient for builds under 5 minutes

Worth the Cost When:

  • Build times exceed 15+ minutes locally
  • CI builds block developer productivity
  • Team has 10+ developers working on monorepo daily

Not Worth It When:

  • Total build time under 5 minutes
  • Small team (< 5 developers)
  • Budget constraints outweigh time savings

Build Performance Optimization

Selective Deployment Configuration:

- name: Detect changes
  uses: dorny/paths-filter@v2
  id: changes
  with:
    filters: |
      api:
        - 'packages/api/**'
      auth:
        - 'packages/auth/**'

- name: Deploy API
  if: steps.changes.outputs.api == 'true'
  run: npx lerna run deploy --scope=@company/api

Critical Warning: Include dependencies with --include-dependencies flag to prevent broken deployments

Production Deployment Failure Modes and Recovery

Zero-Downtime Deployment Requirements

Risk: 5+ minutes of 500 errors during simultaneous package deployment
Solution: Blue-green deployment with staged rollout

Emergency Rollback Strategies:

Infrastructure Rollback (< 2 minutes):

# Kubernetes
kubectl rollout undo deployment/api-service

# Docker containers
docker service rollback api-service

NPM Package Rollback (5-10 minutes):

# Find last working version
git log --oneline --grep="chore(release): publish"

# Revert and republish
git revert <commit-hash>
npx lerna publish from-git --yes

Monitoring and Debugging Production Issues

Problem: 8 packages deployed simultaneously, unclear which broke production
Solution: Package-specific error tracking and semantic versioning tags

Required Tooling:

  • Semantic versioning tags for release tracking
  • Conventional commits for automated changelog generation
  • Package-specific error monitoring (Sentry, DataDog)

Security and Secrets Management

Critical Security Requirements

Failure Modes:

  • API keys in Docker images
  • Database passwords committed to git
  • Auth tokens in package.json

Secure Configuration:

- name: Deploy with secrets
  run: |
    kubectl create secret generic app-secrets \
      --from-literal=DATABASE_URL=${{ secrets.DATABASE_URL }} \
      --from-literal=API_KEY=${{ secrets.API_KEY }}
    
    npx lerna run deploy --scope=@company/api
  env:
    NODE_ENV: production

Rule: Inject secrets at runtime, never at build time

Common Production Deployment Failures

Startup Failures After Successful Deployment

Debug Process:

# Enable verbose Lerna logging
npx lerna run deploy --loglevel silly

# Check for missing environment variables
npx lerna run health-check --parallel

Common Causes:

  • Missing environment variables (database URLs, API keys)
  • Wrong Node.js version in production vs development
  • Dependencies installed with --only=production missing devDependencies used at runtime
  • File paths that work locally but break in containers

Version Conflict Resolution

Development Branch Workflow:

# Creates 1.2.0-beta.0, 1.2.0-beta.1, etc.
npx lerna version --conventional-prerelease --preid beta --yes

Main Branch Workflow:

# Converts 1.2.0-beta.1 to 1.2.0
npx lerna version --conventional-graduate --yes

Critical: Don't manually merge version bumps between branches - Lerna handles version progression automatically

Cost Optimization Strategies

CI/CD Cost Management

Optimization Tactics:

  • GitHub-hosted runners for small builds, self-hosted for heavy workloads
  • Automatic Docker image cleanup: docker system prune --all --filter "until=168h"
  • Artifact retention policies to delete old build artifacts

Warning: Monitor costs continuously - debug logging in production can increase AWS bills from hundreds to thousands without notice

Dependency Change Detection

Correct Change Detection

# Wrong - misses dependent packages
npx lerna run deploy --since HEAD~1

# Right - includes dependencies
npx lerna run deploy --since HEAD~1 --include-dependencies

Critical: Dependency graph detection is core Lerna functionality - if Package A depends on Package B and B changes, both need deployment even if A's code is unchanged

Recovery from Partial Publishing Failures

Scenario: Package publishing fails mid-process, some packages published, others stuck
Solution:

npx lerna publish from-git

Function: Reads git tags to determine what needs publishing, recovers from partial failures automatically

Useful Links for Further Investigation

Resources That Will Save Your Ass

LinkDescription
This NPM E401 Mystery ArticleThis literally saved my ass when auth tokens stopped working after the Nx integration. Covers the `_authToken` vs `npm_config__auth` migration nobody talks about.
GitHub's Official Auth GuideUnlike most blog posts, this actually works. Follow it exactly for npm package publishing in Actions.
TurboRepo Docker GuideBest Docker examples I've found for monorepos. Multi-stage builds and caching that doesn't break every other day.
Official Docker Best PracticesLayer caching strategies that actually improve build times instead of making them worse.
This Stack Overflow ThreadReal examples of selective deployment. Way more useful than theoretical blog posts.
Path Filter ActionThe GitHub Action that prevents deploying unchanged packages. Essential for not breaking things accidentally.
Lerna GitHub IssuesSearch here first when rollbacks fail. The community solutions are often better than the docs.
Blue-Green Deployment GuideThe deployment pattern that lets you sleep at night. Essential reading for production deploys.

Related Tools & Recommendations

alternatives
Recommended

Your Monorepo Builds Take 20 Minutes Because Yarn Workspaces Is Broken

Tools that won't make you want to quit programming

Yarn Workspaces
/alternatives/yarn-workspaces/modern-monorepo-alternatives
100%
tool
Recommended

Yarn Workspaces - Monorepo Setup That Actually Works

Stop wrestling with multiple package.json files and start getting shit done.

Yarn Workspaces
/tool/yarn-workspaces/monorepo-setup-guide
100%
compare
Recommended

Pick Your Monorepo Poison: Nx vs Lerna vs Rush vs Bazel vs Turborepo

Which monorepo tool won't make you hate your life

Nx
/compare/nx/lerna/rush/bazel/turborepo/monorepo-tools-comparison
89%
troubleshoot
Recommended

npm Threw ERESOLVE Errors Again? Here's What Actually Works

Skip the theory bullshit - these fixes work when npm breaks at the worst possible time

npm
/troubleshoot/npm-install-error/dependency-conflicts-resolution
66%
news
Recommended

Major npm Supply Chain Attack Hits 18 Popular Packages

Vercel responds to cryptocurrency theft attack targeting developers

OpenAI GPT
/news/2025-09-08/vercel-npm-supply-chain-attack
66%
tool
Recommended

npm - The Package Manager Everyone Uses But Nobody Really Likes

It's slow, it breaks randomly, but it comes with Node.js so here we are

npm
/tool/npm/overview
66%
troubleshoot
Recommended

Fix Yarn Corepack "packageManager" Version Conflicts

Stop Yarn and Corepack from screwing each other over

Yarn Package Manager
/tool/troubleshoot/yarn-package-manager-error-troubleshooting/corepack-version-conflicts
66%
tool
Recommended

pnpm - Fixes npm's Biggest Annoyances

compatible with pnpm

pnpm
/tool/pnpm/overview
66%
alternatives
Recommended

Turborepo Alternatives - When You're Done With Vercel's Bullshit

Escaping Turborepo hell: Real alternatives that actually work

Turborepo
/alternatives/turborepo/decision-framework
45%
tool
Recommended

Turborepo - Make Your Monorepo Builds Not Suck

Finally, a build system that doesn't rebuild everything when you change one fucking line

Turborepo
/tool/turborepo/overview
45%
tool
Recommended

GitHub Actions Marketplace - Where CI/CD Actually Gets Easier

integrates with GitHub Actions Marketplace

GitHub Actions Marketplace
/tool/github-actions-marketplace/overview
41%
alternatives
Recommended

GitHub Actions Alternatives That Don't Suck

integrates with GitHub Actions

GitHub Actions
/alternatives/github-actions/use-case-driven-selection
41%
integration
Recommended

GitHub Actions + Docker + ECS: Stop SSH-ing Into Servers Like It's 2015

Deploy your app without losing your mind or your weekend

GitHub Actions
/integration/github-actions-docker-aws-ecs/ci-cd-pipeline-automation
41%
tool
Popular choice

jQuery - The Library That Won't Die

Explore jQuery's enduring legacy, its impact on web development, and the key changes in jQuery 4.0. Understand its relevance for new projects in 2025.

jQuery
/tool/jquery/overview
41%
tool
Popular choice

AWS RDS Blue/Green Deployments - Zero-Downtime Database Updates

Explore Amazon RDS Blue/Green Deployments for zero-downtime database updates. Learn how it works, deployment steps, and answers to common FAQs about switchover

AWS RDS Blue/Green Deployments
/tool/aws-rds-blue-green-deployments/overview
39%
tool
Popular choice

KrakenD Production Troubleshooting - Fix the 3AM Problems

When KrakenD breaks in production and you need solutions that actually work

Kraken.io
/tool/kraken/production-troubleshooting
35%
troubleshoot
Popular choice

Fix Kubernetes ImagePullBackOff Error - The Complete Battle-Tested Guide

From "Pod stuck in ImagePullBackOff" to "Problem solved in 90 seconds"

Kubernetes
/troubleshoot/kubernetes-imagepullbackoff/comprehensive-troubleshooting-guide
34%
troubleshoot
Popular choice

Fix Git Checkout Branch Switching Failures - Local Changes Overwritten

When Git checkout blocks your workflow because uncommitted changes are in the way - battle-tested solutions for urgent branch switching

Git
/troubleshoot/git-local-changes-overwritten/branch-switching-checkout-failures
32%
tool
Recommended

NGINX Ingress Controller - Traffic Routing That Doesn't Shit the Bed

NGINX running in Kubernetes pods, doing what NGINX does best - not dying under load

NGINX Ingress Controller
/tool/nginx-ingress-controller/overview
30%
tool
Recommended

Nx - Caches Your Builds So You Don't Rebuild the Same Shit Twice

Monorepo build tool that actually works when your codebase gets too big to manage

Nx
/tool/nx/overview
30%

Recommendations combine user behavior, content similarity, research intelligence, and SEO optimization