Cleanup GitHub caches #543
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cleaning caches | |
# | |
# https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#force-deleting-cache-entries | |
# https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy | |
name: Cleanup GitHub caches | |
on: | |
pull_request: | |
types: | |
- closed | |
workflow_dispatch: | |
schedule: | |
- cron: '27 20 * * *' | |
env: | |
GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}" | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
permissions: | |
# `actions:write` permission is required to delete caches | |
# 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 | |
actions: write | |
contents: read | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
- name: Install prerequisites | |
run: | | |
gh extension install actions/gh-actions-cache | |
- name: Cleanup cache from non default branch | |
if: contains(fromJSON('["schedule", "workflow_dispatch"]'), github.event_name) | |
run: | | |
REPO=${{ github.repository }} | |
# Cannot use github.event.repository.default_branch variable | |
# as it does not work when when running schedule events via cron. | |
# when github.event_name == "schedule" the only element in github.event is schedule (the cron string that triggered the run). | |
DEFAULT_BRANCH="$(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)" | |
CACHES_TO_REMOVE="$(gh actions-cache list -R "${REPO}" --order asc -L 100 |grep -v "${DEFAULT_BRANCH}" | cut -f 1)" | |
## Setting this to not fail the workflow while deleting cache keys. | |
set +e | |
echo "Deleting caches..." | |
for cacheKey in ${CACHES_TO_REMOVE} | |
do | |
gh actions-cache delete "${cacheKey}" -R "${REPO}" --confirm | |
done | |
echo "Done" | |
- name: Cleanup cache from closed pull request | |
if: github.event_name == 'pull_request' | |
run: | | |
REPO=${{ github.repository }} | |
BRANCH=refs/pull/${{ github.event.pull_request.number }}/merge | |
echo "Fetching list of cache key" | |
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 ) | |
## Setting this to not fail the workflow while deleting cache keys. | |
set +e | |
echo "Deleting caches..." | |
for cacheKey in $cacheKeysForPR | |
do | |
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm | |
done | |
echo "Done" |