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 /app/dist ./dist COPY /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 Strategy | No Optimization | With Optimization | Improvement |
|---|---|---|---|
| Store Caching | 45s | 8s | 82% |
| Parallel Build | 120s | 35s | 71% |
| Docker Layering | 180s | 60s | 67% |
| Mirror Acceleration | 60s | 15s | 75% |
Best Practices:
- Always use frozen-lockfile
bashpnpm install --frozen-lockfile
- 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') }}
- 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
- Parallel Execution
bash# Run tests in parallel pnpm -r --parallel test # Build in parallel pnpm -r --parallel build