乐闻世界logo
搜索文章和话题

How to optimize build speed with pnpm in CI/CD environments?

3月5日 23:35

In CI/CD environments, pnpm can optimize build speed through various methods.

Basic Optimization Configuration:

yaml
# .github/workflows/ci.yml name: CI jobs: build: steps: - uses: actions/checkout@v4 - name: Setup pnpm uses: pnpm/action-setup@v2 with: version: 8 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' cache: 'pnpm' # Automatically cache pnpm store - name: Install dependencies run: pnpm install --frozen-lockfile

Store Caching Strategy:

yaml
# GitHub Actions - Manual store caching - name: Cache pnpm store uses: actions/cache@v3 with: path: | ~/.pnpm-store **/node_modules key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }} restore-keys: | ${{ runner.os }}-pnpm-

Docker Optimization:

dockerfile
# Dockerfile FROM node:20-alpine # Install pnpm RUN npm install -g pnpm # Copy lock file first (utilize cache layer) COPY pnpm-lock.yaml ./ COPY package.json ./ # Install dependencies RUN pnpm install --frozen-lockfile --prod # Copy source code COPY . . # Build RUN pnpm build

Multi-stage Build Optimization:

dockerfile
# Build stage FROM node:20-alpine AS builder RUN npm install -g pnpm WORKDIR /app COPY pnpm-lock.yaml package.json ./ RUN pnpm install --frozen-lockfile COPY . . RUN pnpm build # Production stage FROM node:20-alpine RUN npm install -g pnpm WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules COPY package.json ./ CMD ["node", "dist/main.js"]

Parallel Execution Optimization:

yaml
# GitLab CI stages: - install - test - build install: stage: install script: - pnpm install --frozen-lockfile cache: key: ${CI_COMMIT_REF_SLUG} paths: - .pnpm-store - node_modules test: stage: test script: - pnpm test parallel: 4 # Parallel testing build: stage: build script: - pnpm build

Monorepo Optimization:

yaml
# Only build changed packages - name: Build changed packages run: pnpm -r --filter "...[origin/main]" build # Parallel build - name: Parallel build run: pnpm -r --parallel build # Topological build - name: Topological build run: pnpm -r --workspace-concurrency=4 build

Network Optimization:

ini
# .npmrc # Use China mirror registry=https://registry.npmmirror.com/ # Increase concurrency network-concurrency=32 # Increase timeout fetch-timeout=120000 # Increase retry count fetch-retries=5

Installation Optimization Parameters:

bash
# Frozen lockfile (faster, safer) pnpm install --frozen-lockfile # Prefer offline cache pnpm install --prefer-offline # Ignore engines check (faster) pnpm install --ignore-engines # No progress bar (slightly faster) pnpm install --reporter=silent

Performance Comparison:

Optimization StrategyNo OptimizationWith OptimizationImprovement
Store Caching45s8s82%
Parallel Build120s35s71%
Docker Layering180s60s67%
Mirror Acceleration60s15s75%

Best Practices:

  1. Always use frozen-lockfile
bash
pnpm install --frozen-lockfile
  1. Caching Strategy
yaml
# Cache pnpm store and node_modules - uses: actions/cache@v3 with: path: | ~/.pnpm-store node_modules key: pnpm-${{ hashFiles('pnpm-lock.yaml') }}
  1. Layered Build
dockerfile
# Copy dependency-related files first COPY pnpm-lock.yaml package.json ./ RUN pnpm install --frozen-lockfile # Then copy source code COPY . . RUN pnpm build
  1. Parallel Execution
bash
# Run tests in parallel pnpm -r --parallel test # Build in parallel pnpm -r --parallel build
标签:PNPM