actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.45k stars 1.18k forks source link

"Force deletion of caches overriding default cache eviction policy" example code can delete the wrong branch's cache #1107

Closed mikeage closed 1 year ago

mikeage commented 1 year ago

The page at https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy gives an example of deleting a cache created on a feature branch when the PR is merged, to prevent the feature branch's caches from taking up space and potentially evicting the cache from the master/main branch. The example code triggers on:

on:
  pull_request:
    types:
      - closed

and runs against:

...
          REPO=${{ github.repository }}
          BRANCH=${{ github.ref }}

The documentation at https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request suggests that $GITHUB_REF / ${{ github.ref }} should contain refs/pull/:prNumber/merge.

However, in my testing, this is only true if the PR is closed without being merged. In that case, it does, indeed, point to refs/pull/:prNumber/merge. If the PR is merged, however, then the value of ${{ github.ref }} is the target branch, e.g., main. As such, this code will then clear the main branch's cache, which is not only pointless, but harmful!

As such, in our code at https://github.com/icosa-gallery/open-brush/pull/397, I changed it to use ${{ format('refs/pull/{0}/merge', github.event.number) }}. This behaves correct in both the regular close, and close-on-merge cases.

The root cause may a general github actions issue, but until that's confirmed or fixed or clarified, I think the example should be modified accordingly.

t-dedah commented 1 year ago

Hi @mikeage thank you for catching this. We have raised a PR for updating the docs with BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge

https://github.com/actions/cache/pull/1108