| 1 | # https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy |
| 2 | |
| 3 | name: (Shared) Cleanup Merged Branch Caches |
| 4 | on: |
| 5 | pull_request: |
| 6 | types: |
| 7 | - closed |
| 8 | workflow_dispatch: |
| 9 | inputs: |
| 10 | pr_number: |
| 11 | required: true |
| 12 | type: string |
| 13 | |
| 14 | permissions: {} |
| 15 | |
| 16 | jobs: |
| 17 | cleanup: |
| 18 | runs-on: ubuntu-latest |
| 19 | permissions: |
| 20 | # `actions:write` permission is required to delete caches |
| 21 | # See also: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id |
| 22 | actions: write |
| 23 | contents: read |
| 24 | steps: |
| 25 | - name: Cleanup |
| 26 | run: | |
| 27 | echo "Fetching list of cache key" |
| 28 | cacheKeysForPR=$(gh cache list --ref $BRANCH --limit 100 --json id --jq '.[].id') |
| 29 | |
| 30 | ## Setting this to not fail the workflow while deleting cache keys. |
| 31 | set +e |
| 32 | for cacheKey in $cacheKeysForPR |
| 33 | do |
| 34 | gh cache delete $cacheKey |
| 35 | echo "Deleting $cacheKey" |
| 36 | done |
| 37 | echo "Done" |
| 38 | env: |
| 39 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 40 | GH_REPO: ${{ github.repository }} |
| 41 | BRANCH: refs/pull/${{ inputs.pr_number || github.event.pull_request.number }}/merge |