Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache miss for existing keys when merging PRs #1561

Open
hammzj opened this issue Feb 26, 2025 · 3 comments
Open

Cache miss for existing keys when merging PRs #1561

hammzj opened this issue Feb 26, 2025 · 3 comments

Comments

@hammzj
Copy link

hammzj commented Feb 26, 2025

I have a workflow that is saving a cached file from the head branch to the base branch on a pull request merge. The file keys exists on the head branch, but when it attempts to find it using a restore-keys basic path, it is failing to hit. I see the key exists in my GitHub cache for the head branch, so not sure why it's showing as missing.

Here is an example workflow:

name: Cache file on base branch
on:
  pull_request:
    types: [closed]

env:
  node-version: 20.18.x

jobs:
  cache_file:
    # this job will only run if the PR has been merged
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo PR #${{ github.event.number }} has been merged

      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.node-version }}

      - name: Restore file from head branch

        uses: actions/cache/restore@v4
        with:
          fail-on-cache-miss: true
          path: main.json
          key:  file-${{ github.head_ref }}-${{ github.run_id }}-${{ github.run_attempt }}
          restore-keys: |
            file-${{ github.head_ref }}-
 
      - name: Save file to base ref
        uses: actions/cache/save@v4
        with:
          path: main.json
          key: file-${{ github.base_ref }}-${{ github.run_id }}-${{ github.run_attempt }}
@AlexanderRichert-NOAA
Copy link

I seem to be running into the same issue. As I tried with a few different actions/cache version, I ended up in a situation where it wouldn't save the cache because it already exists, but there are no caches listed on the caches page for my repo.

@hammzj
Copy link
Author

hammzj commented Feb 27, 2025

Ah, I see this in GitHub's documentation:

Caching dependencies to speed up workflows

When a cache is created by a workflow run triggered on a pull request, the cache is created for the merge ref (refs/pull/.../merge). Because of this, the cache will have a limited scope and can only be restored by re-runs of the pull request. It cannot be restored by the base branch or other pull requests targeting that base branch.

I've decided to use a 3rd party action which allows this to work correctly, but by downloading artifacts from other workflows instead of accessing cache: https://github.com/dawidd6/action-download-artifact

This looks like the path others took as well, but this is a serious limitation of the cache feature. I understand not being able to access child or sibling branches, but it's a glaring issue if a base branch cannot access the cache of the source merged into it.

@AlexanderRichert-NOAA
Copy link

AlexanderRichert-NOAA commented Feb 27, 2025

For me this is all on the same branch, so maybe I'm running into a separate issue. (edit: apparently the issue is that I was using different file paths between my restore and save steps)

KtorZ added a commit to KtorZ/cache that referenced this issue Mar 7, 2025
Thanks to this comment which saved me some headache: actions#1491 (comment)

related to actions#1561.
related to actions#1491.
related to actions#1426.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants