cache icon indicating copy to clipboard operation
cache copied to clipboard

Cache Not Restored if Created on Base Branch

Open daniel-nagy opened this issue 2 years ago • 2 comments

Given the following branches,

main -> base-1 -> base-2

Caches created on base-1 cannot be restored on base-2. However, according to the documentation it sounds like this should work.

A workflow can access and restore a cache created in the current branch, the base branch (including base branches of forked repositories), or the default branch (usually main)

This is enough to reproduce.

name: GH Actions Test
on:
  pull_request:
  push:
    branches:
      - main
jobs:
  test:
    env:
      BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || '' }}
      HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
    runs-on: ubuntu-latest
    steps:
      - name: try to restore cache from base branch
        if: github.event_name == 'pull_request'
        id: restore-cache
        uses: actions/cache@v3
        with:
          path: ${{ env.BASE_SHA }}
          key: ${{ runner.os }}-${{ env.BASE_SHA }}
      - name: cache
        uses: actions/cache@v3
        with:
          path: ${{ env.HEAD_SHA }}
          key: ${{ runner.os }}-${{ env.HEAD_SHA }}
      - name: debug
        run: echo ${{ steps.restore-cache.outputs.cache-hit }}
      - name: create file
        run: |
          mkdir $HEAD_SHA 2>/dev/null && echo 'foo' > "$HEAD_SHA/foo.txt"

base-1 is able to restore the cache create by main but base-2 is not able to restore the cache created by base-1.

daniel-nagy avatar Jun 14 '22 22:06 daniel-nagy

Hello @daniel-nagy . I faced the same problem. I think this comment is the answer.

link: https://github.com/actions/cache/issues/843#issuecomment-1179771256

guppy0356 avatar Jul 15 '22 01:07 guppy0356

This will require a docs change. We are going to update it. Thank you for reporting. #877 and #843 also have the same issue. I've closed those and using this issue for tracking.

vsvipul avatar Aug 04 '22 12:08 vsvipul

@vsvipul are there any big technical challenges about enabling cache restore from base branch, not only default? At my workplace we use release/... branches and merge PRs into them instead of main, thus every time a new PR is created, the cache is not found.

I feel like changing the documentation isn't really a solution, but a workaround)

Logarithmus avatar Nov 09 '22 13:11 Logarithmus

@Logarithmus There is some technical challenge in understanding the base branch for any branch so for now we have updated the docs to address this behaviour.

Phantsure avatar Nov 16 '22 08:11 Phantsure

Closing this issue as public docs are updated: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache @Logarithmus You may open a new issue with the expected behaviour. We can track that separately

Phantsure avatar Nov 16 '22 08:11 Phantsure