Search code examples
githubgithub-actionscicdpnpm

PNPM Github actions flow


I just moved my project from npm and lerna to pnpm but now when using GitHub actions I get the following error

"line 1: pnpm: command not found"

can someone suggest how the .yml file should be, I've posted the current version below?

name: Lint & Unit Test

on: [pull_request]

jobs:
  run-linters:
    name: Run linter and Unit tests
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v3

      - name: ACTIONS_ALLOW_UNSECURE_COMMANDS
        run: echo 'ACTIONS_ALLOW_UNSECURE_COMMANDS=true' >> $GITHUB_ENV

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16.18.1

      - name: Portal Install Node.js dependencies
        working-directory: ./portals
        run: |
          pnpm install

      - name: Portals Lint & tests
        working-directory: ./portals
        run: |
          cat .env.example > .env
          pnpm run build:tailwind
          pnpm run lint
          pnpm test

      - name: Services Install Node.js dependencies
        working-directory: ./services
        run: |
          pnpm install

      - name: Services Lint & tests
        working-directory: ./services
        run: |
          pnpm run lint
          pnpm test

Solution

  • pnpm is a fast and efficient package manager for Node.js, similar to npm and yarn. It is known for saving disk space and speeding up installations.

    To set up pnpm in a GitHub Actions workflow, in the GitHub Actions workflow file (usually .github/workflows/<your-workflow>.yml), add a step to install pnpm: use the pnpm/action-setup action to do this. It automatically installs pnpm in your GitHub Actions runner environment.

    on:
        push:
        pull_request:
    
    jobs:
        install:
        runs-on: ubuntu-latest
    
        steps:
            - uses: actions/checkout@v3
            
            - name: Setup pnpm
            uses: pnpm/action-setup@v3 # docs https://pnpm.io/continuous-integration#github-actions
            with:
                version: 8  # Optional: specify a pnpm version
    
            # Further steps for your build/test process
    

    Specifying a pnpm version is optional. If omitted, the latest version is installed. If you want to make sure consistency with your local development environment, specify the same pnpm version that you use locally.


    Do you have to specify a version? Do I have to grub around in my yaml every time pnpm releases a version ?

    The version is optional when there is a packageManager field in the package.json.

    {
      "name": "your-project",
      "version": "1.0.0",
      "packageManager": "[email protected]",
      // other fields
    }
    

    Corepack is an experimental Node.js tool that provides a way to manage multiple package managers (like npm, yarn, and pnpm). It can automatically install the correct version of these package managers based on the configuration specified in a project's package.json.

    Corepack will make sure the version of pnpm used in your GitHub Actions workflow matches the version specified in your package.json. That will align your local development environment with the CI/CD environment, maintaining consistency across different environments.