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:
- Update
@company/utils
from 1.2.0 to 1.3.0 - Update
@company/ui
to depend on@company/utils@^1.3.0
- Publish
@company/ui@2.1.0
first (CRITICAL ERROR) - Users install UI 2.1.0, get dependency error for utils 1.3.0
- 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
Link | Description |
---|---|
This NPM E401 Mystery Article | This 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 Guide | Unlike most blog posts, this actually works. Follow it exactly for npm package publishing in Actions. |
TurboRepo Docker Guide | Best Docker examples I've found for monorepos. Multi-stage builds and caching that doesn't break every other day. |
Official Docker Best Practices | Layer caching strategies that actually improve build times instead of making them worse. |
This Stack Overflow Thread | Real examples of selective deployment. Way more useful than theoretical blog posts. |
Path Filter Action | The GitHub Action that prevents deploying unchanged packages. Essential for not breaking things accidentally. |
Lerna GitHub Issues | Search here first when rollbacks fail. The community solutions are often better than the docs. |
Blue-Green Deployment Guide | The deployment pattern that lets you sleep at night. Essential reading for production deploys. |
Related Tools & Recommendations
Your Monorepo Builds Take 20 Minutes Because Yarn Workspaces Is Broken
Tools that won't make you want to quit programming
Yarn Workspaces - Monorepo Setup That Actually Works
Stop wrestling with multiple package.json files and start getting shit done.
Pick Your Monorepo Poison: Nx vs Lerna vs Rush vs Bazel vs Turborepo
Which monorepo tool won't make you hate your life
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
Major npm Supply Chain Attack Hits 18 Popular Packages
Vercel responds to cryptocurrency theft attack targeting developers
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
Fix Yarn Corepack "packageManager" Version Conflicts
Stop Yarn and Corepack from screwing each other over
pnpm - Fixes npm's Biggest Annoyances
compatible with pnpm
Turborepo Alternatives - When You're Done With Vercel's Bullshit
Escaping Turborepo hell: Real alternatives that actually work
Turborepo - Make Your Monorepo Builds Not Suck
Finally, a build system that doesn't rebuild everything when you change one fucking line
GitHub Actions Marketplace - Where CI/CD Actually Gets Easier
integrates with GitHub Actions Marketplace
GitHub Actions Alternatives That Don't Suck
integrates with GitHub Actions
GitHub Actions + Docker + ECS: Stop SSH-ing Into Servers Like It's 2015
Deploy your app without losing your mind or your weekend
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.
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
KrakenD Production Troubleshooting - Fix the 3AM Problems
When KrakenD breaks in production and you need solutions that actually work
Fix Kubernetes ImagePullBackOff Error - The Complete Battle-Tested Guide
From "Pod stuck in ImagePullBackOff" to "Problem solved in 90 seconds"
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
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
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
Recommendations combine user behavior, content similarity, research intelligence, and SEO optimization