in Code

Yarn Modern (2+) and GitHub Actions

January 29, 2024

I've really enjoyed using GitHub Actions as a method to run CI for my projects. The integration with GitHub is fairly smooth, and once you get it setup, incorporating rules to prevent PR merges without green builds is pretty easy. This has all worked well up until I needed to upgrade Yarn (for reasons to do with Storybook). The process was fairly straightforward, but there were some issues getting GitHub Actions to play nice.

Upgrading Yarn

The first step is upgrading Yarn. There's documentation on the Yarn site on how to do this, but essentially it comes down to the following:

  1. Make sure you're using Node 18+
  2. Run corepack enable to activate Corepack
  3. Run yarn set version stable to use the latest version of Yarn
  4. Run yarn install to migrate the lockfile

Since I was updating from a project that used node_modules, I felt it was simpler to continue to use that same format. So in a (newly created) .yarnrc.yml file I added the following:

# .yarnrc.yml

nodeLinker: node-modules

With this complete, my package.json file had a new entry to specify that my project used "packageManager": "yarn@4.0.2" (this is most likely a newer version in your project).

Updating GitHub Actions

With the Yarn upgrade complete, it's time to turn our attention to GitHub Actions. At a high level, the important changes we need to make are:

Here's a sample GitHub Actions yml file to demonstrate:

# ci.js.yml

name: CI

    branches: [main]
    branches: [main]

    runs-on: ubuntu-latest

      - uses: actions/checkout@v4
      - name: Enable Corepack
        run: corepack enable
      - name: Use Node.js 20.x
        uses: actions/setup-node@v4
          node-version: "20.x"
          cache: 'yarn'
      - name: Install dependencies
        run: yarn install --immutable
      - name: Run lint
        run: yarn lint
      - name: Run tests
        run: yarn test
      - name: Run build
        run: yarn build

More details on the actions I took can be found in this PR: https://github.com/dylants/bookstore/pull/26/files