diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index fd6bc47e..1e8da6ba 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -1,8 +1,12 @@ name: Cleanup caches for closed PRs + on: - pull_request: - types: - - closed + # Run twice every day to remove the cache so that the caches from the closed prs + # are removed. + schedule: + - cron: "0 17 * * *" + - cron: "30 18 * * *" + workflow_dispatch: jobs: cleanup: @@ -13,26 +17,12 @@ jobs: run: | gh extension install actions/gh-actions-cache - REPO="${{ github.repository }}" - BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge" + export REPO="${{ github.repository }}" - while true; do - echo "Fetching list of cache key" - cacheKeysForPR="$(gh actions-cache list -R "$REPO" -B "$BRANCH" -L 100 | cut -f 1 )" - - if [ -z "$cacheKeysForPR" ]; then - break - fi - - ## Setting this to not fail the workflow while deleting cache keys. - set +e - echo "Deleting caches..." - for cacheKey in $cacheKeysForPR - do - echo Removing "$cacheKey" - gh actions-cache delete "$cacheKey" -R "$REPO" -B "$BRANCH" --confirm + gh pr list --state closed -L 20 --json number --jq '.[]|.number' | ( + while IFS='$\n' read -r closed_pr; do + BRANCH="refs/pull/${closed_pr}/merge" ./cleanup-cache.sh done - done - echo "Done" + ) env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/cleanup-cache.sh b/cleanup-cache.sh new file mode 100755 index 00000000..5c38d86a --- /dev/null +++ b/cleanup-cache.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +set -uxo pipefail + +REPO="${REPO?}" +BRANCH="${BRANCH?}" + +while true; do + echo "Fetching list of cache key for $BRANCH" + cacheKeysForPR="$(gh actions-cache list -R "$REPO" -B "$BRANCH" -L 100 | cut -f 1 )" + + if [ -z "$cacheKeysForPR" ]; then + break + fi + + ## Setting this to not fail the workflow while deleting cache keys. + set +e + echo "Deleting caches..." + for cacheKey in $cacheKeysForPR + do + echo Removing "$cacheKey" + gh actions-cache delete "$cacheKey" -R "$REPO" -B "$BRANCH" --confirm + done +done +echo "Done cleaning up $BRANCH"