actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.51k stars 1.2k forks source link

cache key not found when restoring on dependant job #1128

Closed GaxZE closed 11 months ago

GaxZE commented 1 year ago

I am trying to run a workflow with a caching strategy that starts with a setup job and then caches the dependencies and restores them being running a linting job.

name: Main

on:
  pull_request:
    branches: [main]

jobs:
  setup:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x]
    steps:
      - uses: actions/checkout@v3
      - run: npm ci
      - run: npm run build-stylable-types
      - uses: actions/cache/save@v3
        id: npm-cache
        with:
          path: |
            ~/.npm
            node_modules
          key: ${{ matrix.node-version }}-npm-cache-${{ hashFiles('**/package-lock.json') }}

  lint:
    runs-on: ubuntu-latest
    needs: setup
    strategy:
      matrix:
        node-version: [16.x, 18.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - uses: actions/cache/restore@v3
        id: npm-cache
        with:
          path: |
            ~/.npm
            node_modules
          key: ${{ matrix.node-version }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      - run: npm run lint

Jobs end up with following:

Setup:

Screenshot 2023-03-03 at 10 45 57

Build:

Screenshot 2023-03-03 at 10 45 23

Key is 16.x-npm-cache-8653ab79e0fd3bd57121958ffb101b35fc1f86cc4c053b96ff238091a01df3ae (also 18.x-npm-cache-8653ab79e0fd3bd57121958ffb101b35fc1f86cc4c053b96ff238091a01df3ae

Have I missed a part of the docs where the cache can't persist to another job? The Pull request it's coming from is a fork PR. I am trying to understand the scope to be able to restore. I suspect my issue is there. But I did also try this as a branch within the repository instead of a forked one.

shealavington commented 1 year ago

You're running the cache after instead of before the npm i, try adding the cache before you create the things you want to cache.

I also suggest adding the following to the restore action so the restore fails on the correct part

with:
  fail-on-cache-miss: true

Try this:

name: Main

on:
  pull_request:
    branches: [main]

jobs:
  setup:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x, 18.x]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: npm-cache
        with:
          path: |
            ~/.npm
            node_modules
          key: ${{ matrix.node-version }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      - run: npm ci
        if: steps.npm-cache.outputs.cache-hit != 'true'
      - run: npm run build-stylable-types
        if: steps.npm-cache.outputs.cache-hit != 'true'

  lint:
    runs-on: ubuntu-latest
    needs: setup
    strategy:
      matrix:
        node-version: [16.x, 18.x]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache/restore@v3
        id: npm-cache
        with:
          fail-on-cache-miss: true
          path: |
            ~/.npm
            node_modules
          key: ${{ matrix.node-version }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm run lint
GaxZE commented 1 year ago

You're running the cache after instead of before the npm i, try adding the cache before you create the things you want to cache.

Thanks for the response. I was actually running it after npm ci which will remove the cache I am restoring. I seemingly solved this issue by removing ~/.npm in the path and used absolute paths /home/runner/.npm for example.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open for 200 days with no activity. Leave a comment to avoid closing this issue in 5 days.

github-actions[bot] commented 11 months ago

This issue was closed because it has been inactive for 5 days since being marked as stale.