Cache V2 doesn't fetch cache from parent or default branch

As per the documentation: https://docs.github.com/en/free-pro-team@latest/actions/guides/caching-dependencies-to-speed-up-workflows

Also, if the branch feature-b has the base branch feature-a , a workflow triggered on feature-b would have access to caches created in the default branch ( main ), feature-a , and feature-b

Doesn’t this apply to Workflows that are triggered on Pull Requests?

  - name: My Cache Step
    uses: actions/cache@v2
    with:
      path: ${{ env.CACHE_PATH }}
      key: ${{ env.CACHE_KEY }}

The above is my cache step. Nothing fancy about it. I just use a KEY to cache a 40mb file. The problem is every time I raise a PR, I get a “Cache not found for input keys”.
I raised a PR from feature-a to staging. And staging to main. main is my default branch. On every one of these PR, for the Same KEY, the cache is created new.
Now, when I raise a PR from feature-b to staging or main, with the same KEY, I expect the cache to be already present. But it’s not. It’s created again.
I am a bit confused. Am I missing something?

1 Like

@thsaravana,

Doesn’t this apply to Workflows that are triggered on Pull Requests?

Sure, it also applied to workflows for pull request. More details, you can see here.

From the description about “Restrictions for accessing a cache”, we can see:

Access restrictions provide cache isolation and security by creating a logical boundary between different workflows and branches. For example, a cache created for the branch feature-a (with the base main) would not be accessible to a pull request for the branch feature-b (with the base main).

In your case, main is the default branch and the base branch of staging, staging is the base branch of feature-a.

  1. If the cache is created in the workflow for feature-a, or the PR from feature-a to another branch (staging or main), the following situations can’t access the cache:

    • Workflows run on staging or main.
    • Workflows for the PR from staging into main.
  2. If the cache is created in the workflow for staging, or the PR from staging to main, the following situations can’t access the cache:

    • Workflows run on main.
    • Workflows for the PR from feature-a into main.

In addition, when you use the cache action to create cache in the workflow, you should make sure the path you specify is the correct directory where the dependencies actually are installed, and the directory should not be empty.
The cache action can create the cache from a directory that is existing but empty, and will not return any warning or error in the logs. The cache with an empty directory may cause some unexpected troubles, when you use it in the subsequent workflow runs.

@brightran
First up, my cache is getting created properly with no issues. I am pointing to a correct directory that is not empty and holds a single file. I have no issues here.

My problem is when fetching the cache. In the below cases, I expect the cache to be present as per the documentation. But it isn’t.

  1. PR from “feature-b” to “staging”
  2. PR from “staging” to “main”
  3. PR from “feature-b” to “main”

I have already created a PR from “feature-a” to “staging”. Merged that with “staging”. Created a PR from “staging” to “main” and merged that with “main”.

In all these cases (both feature-a and feature-b) : my CacheKey is the same - a constant string “tag45”

Note: In the cases 1, 2, and 3. The cache is empty on the first run. But when I further push a commit to trigger the workflow again, the cache is available, and is fetched properly.
So the first time I create a PR, irrespective from which branch to which, the cache is always empty. But post that, when I push further commits on to the same PR, the cache is available. That’s my problem.