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

How to use pnpm instead of npm in Github Actions

1个答案

1

Using pnpm instead of npm in GitHub Actions involves the following steps:

  1. Set up the Node.js environment: In the GitHub Actions workflow, first use actions/setup-node to configure the Node.js environment.

  2. Install pnpm: After setting up the Node.js environment, proceed to install pnpm.

  3. Run commands with pnpm: Once pnpm is 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:

yaml
name: 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@v2 to checkout the code.
  • Configuring the Node.js environment with actions/setup-node@v2 and specifying pnpm as the cache method.
  • Installing pnpm.
  • Using pnpm to install project dependencies.
  • Finally, using pnpm to 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.

2024年6月29日 12:07 回复

你的答案