Using pnpm instead of npm in GitHub Actions involves the following steps:
-
Set up the Node.js environment: In the GitHub Actions workflow, first use
actions/setup-nodeto configure the Node.js environment. -
Install pnpm: After setting up the Node.js environment, proceed to install
pnpm. -
Run commands with pnpm: Once
pnpmis installed, you can utilize it to install dependencies, execute scripts, and perform other tasks.
Here is a basic example demonstrating how to use pnpm in GitHub Actions:
yamlname: Node.js CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [14.x, 16.x] steps: - uses: actions/checkout@v2 # Checkout code - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} cache: 'pnpm' # This enables caching of pnpm's store - name: Install pnpm run: npm install -g pnpm # Install pnpm - name: Install dependencies run: pnpm install # Install project dependencies - name: Run tests run: pnpm test # Run test scripts
This workflow file defines a CI workflow that triggers on pushes to the main branch or pull requests targeting the main branch. The workflow includes:
- Using
actions/checkout@v2to checkout the code. - Configuring the Node.js environment with
actions/setup-node@v2and specifyingpnpmas the cache method. - Installing
pnpm. - Using
pnpmto install project dependencies. - Finally, using
pnpmto execute test scripts.
This workflow runs across two Node.js versions—14.x and 16.x—to ensure cross-version compatibility.
Note that starting with setup-node@v2, GitHub Actions provides native support for caching pnpm, allowing you to leverage this feature by setting the cache option to accelerate subsequent workflow runs. Prior to this version, you would need to cache the pnpm store folder separately.