Phil Lucks
Phil Lucks

Reputation: 3913

Github Actions Reusable workflows yarn cache not found

I am new to writing GH Actions. I am working on a task to remove common workflows and use the reusable workflow feature available. I am now able to get my workflows to run sequentially which is great. However, the 2nd workflow is resulting in an unexpected error message seemingly related to the yarn dependency workflow not saving to the cache as I would have expected it:

Run yarn lint ... snip myPackage: /bin/sh: 1: concurrently: not found

Could you take a look at see if this looks ok? For now, my goal is to have a workflow for pull-request which calls yarn and lint as the resuable features:

name: pull-request
on:
  pull_request:
    branches:
      - main
jobs:
  yarn:
    uses: ./.github/workflows/yarn.yml
  validate_lint:
    needs: yarn
    uses: ./.github/workflows/validate_lint.yml

with:

name: Yarn

on:
  workflow_call:

jobs:
  yarn_and_deps:
    name: Run Lint
    runs-on: ubuntu-latest

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

      - name: Enable node
        uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v3
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install dependencies
        run: yarn install --frozen-lockfile && yarn bootstrap
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

and

name: Validate Lint

on:
  workflow_call:

jobs:
  run_lint:
    name: Run Lint
    runs-on: ubuntu-latest

    - name: Enable node
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: 'yarn' # <<--- THIS CACHE IS NOT FOUND 🤷🏻‍♂️

    # NOTE: if I add in all the "yarn cache/install" commands from above workflow, this passes.

    steps:
      - name: Validate Lint
        run: yarn lint

The error happens here in the Validate Lint job because it appears that the cache is not found. I made the yarn job to avoid re-creating the wheel for each job.

What is wrong with my expectations on the cache v how it actually works? Having to Install dependencies step each job feels like overkill.

Upvotes: 5

Views: 1884

Answers (1)

Phil Lucks
Phil Lucks

Reputation: 3913

Turns out each workflow is its own docker container. Therefore, if I run yarn in workflow 1's container, workflow 2 has no knowledge/access to the cache.
The closest thing appears to be an upload/download "sharing of data", but this has it's own drawbacks - such as downloading a node_modules folder can be slower than just installing the dependencies.

Unfortunately, the solution seems to be that there is repetition of code when each workflow has a dependency to the output of prior item.

Upvotes: 4

Related Questions