From 47d4aeaa96cd478fdd6cc97165b70240da44497b Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 23 Mar 2023 18:10:30 +1100 Subject: [PATCH] Run cache-cleanup at 3am & 4:30am AEST instead of after every PR (#944) since cache-cleanup will issue a lot of gh api requests, running the workflow right after the PRs would cause the GH API to rate limit all workflows (including our CI for testing and release) that they can no longer upload artifacts and `cargo-binstall` would have to fallback to sending GET requests instead of using GH API, which makes it a lot slower and more likely to fail. This PR changes it to be run at 3am and 4:30am AEST which nobody would submit any PR at that time and then remove all caches of the top 20 closed prs. The workflow can also now be triggered manually. Signed-off-by: Jiahao XU --- .github/workflows/cache-cleanup.yml | 34 ++++++++++------------------- cleanup-cache.sh | 25 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 22 deletions(-) create mode 100755 cleanup-cache.sh 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"