diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index 1e8da6ba..2a9ad231 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -15,14 +15,26 @@ jobs: - uses: actions/checkout@v3 - name: Cleanup run: | + set -euxo pipefail + gh extension install actions/gh-actions-cache export REPO="${{ github.repository }}" + # Setting this to not fail the workflow while deleting cache keys. + set +e + + # Remove pull requests cache, since they cannot be reused 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 ) + # Remove merge queue cache, since they cannot be reused + gh actions-cache list -L 100 | cut -f 3 | grep 'gh-readonly-queue' | sort -u | ( + while IFS='$\n' read -r branch; do + BRANCH="$branch" ./cleanup-cache.sh + done + ) env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/cleanup-cache.sh b/cleanup-cache.sh index 5c38d86a..4fcbf6d3 100755 --- a/cleanup-cache.sh +++ b/cleanup-cache.sh @@ -7,14 +7,12 @@ 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 )" + 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