Skip to content

Instantly share code, notes, and snippets.

@ThatGuySam
Forked from belgattitude/ci-pnpm-install.md
Created June 10, 2023 20:23
Show Gist options
  • Select an option

  • Save ThatGuySam/663e2d7b798bbedc41a0ff150858ba2c to your computer and use it in GitHub Desktop.

Select an option

Save ThatGuySam/663e2d7b798bbedc41a0ff150858ba2c to your computer and use it in GitHub Desktop.
Composite github action to improve CI time with pnpm

Not using pnpm ?, see the corresponding yarn gist

Why

While @setup/node has a built-in cache parameter for popular package managers, it discards the cache on every lock file update. This composite action allows to run install with (almost always) warm cache.

That will reduce the monthly ci-time and decrease the carbon emissions. See also actions/setup-node#325.

Bench

Based on the nextjs-monorepo-example with pnpm.

A cold cache install on the ci is around ±1m20s.

With warmed cache: ±40s + (add ±10s for compression). Crafted from benchmarks results in https://gist.github.com/belgattitude/0ecd26155b47e7be1be6163ecfbb0f0b. Depending on repo (renovatebot...), the slight complexity increase in ci setup might worth it.

Structure

.
└── .github
    ├── actions
    │   └── pnpm-install/action.yml (composite action)    
    └── workflows
        └── ci.yml (uses: ./.github/actions/pnpm-install)    

Composite action

Create a file in .github/actions/pnpm-install/action.yml and paste

#######################################################################################
# "pnpm install" composite action                                                      #
#--------------------------------------------------------------------------------------#
#   - bench: https://gist.github.com/belgattitude/0ecd26155b47e7be1be6163ecfbb0f0b     #
########################################################################################

name: 'Monorepo install (pnpm)'
description: 'Run pnpm install'
inputs:
  skip-prisma-postinstall-generate:
    description: 'Avoid prisma to automatically generate schema on postinstall'
    required: false
    default: 'true'
  playwright-skip-browser-download:
    description: 'Avoid playwright to download browsers automatically'
    required: false
    default: '1'

runs:
  using: 'composite'

  steps:
    - uses: pnpm/[email protected]

    - name: Get pnpm store directory
      shell: bash
      id: pnpm-cache
      run: |
        echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"

    - uses: actions/cache@v3
      name: Setup pnpm cache
      with:
        path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
        key: ${{ runner.os }}-pnpm-store-cache-${{ hashFiles('**/pnpm-lock.yaml') }}
        restore-keys: |
          ${{ runner.os }}-pnpm-store-cache-

    # Prevent store to grow over time (not needed with yarn)
    - name: Prune pnpm store
      shell: bash
      run: pnpm prune store

    - name: Install dependencies
      shell: bash
      run: |
        pnpm install --frozen-lockfile
      env:
        # Other environment variables
        HUSKY: '0' # By default do not run HUSKY install
        PRISMA_SKIP_POSTINSTALL_GENERATE: ${{ inputs.skip-prisma-postinstall-generate }}
        PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: ${{ inputs.playwright-skip-browser-download }}

Workflow action

To use it in the workflows

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: 📥 Monorepo install
        uses: ./.github/actions/pnpm-install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment