From fc07d65b5db7ef611897f67841d7c9fd6538e2ff Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:43:44 +0100 Subject: [PATCH 1/6] cleanup caches for merged prs --- .github/workflows/cleanup-cache.yml | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/cleanup-cache.yml diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml new file mode 100644 index 0000000000..b762e56ec6 --- /dev/null +++ b/.github/workflows/cleanup-cache.yml @@ -0,0 +1,52 @@ +# Copied from https://github.com/actions/cache/blob/main/tips-and-workarounds.md#force-deletion-of-caches-overriding-default-cache-eviction-policy +name: Cleanup Caches for Merged PRs + +on: + pull_request: + types: [closed, enqueued] + workflow_dispatch: + inputs: + pr_number: + description: 'PR number to clean up caches for' + required: true + type: number + +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: Cleanup caches + run: | + gh extension install actions/gh-actions-cache + + REPO="${{ github.repository }}" + + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + PR_NUMBER="${{ github.event.pull_request.number }}" + else + PR_NUMBER="${{ github.event.inputs.pr_number }}" + fi + + BRANCH="refs/pull/$PR_NUMBER/merge" + + echo "Fetching list of cache keys for PR #$PR_NUMBER" + 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 "Cache cleanup completed" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 21bd80e911c721390a60dbef7853b8a27fd7d6b4 Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:46:29 +0100 Subject: [PATCH 2/6] space Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com> --- .github/workflows/cleanup-cache.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index b762e56ec6..9fd846c3f0 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -15,8 +15,8 @@ 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 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: From aec2769238801a56fad47b1ca2c6298b64e22659 Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:46:37 +0100 Subject: [PATCH 3/6] force trigger Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com> --- .github/workflows/cleanup-cache.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index 9fd846c3f0..ba3163dc36 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -4,6 +4,9 @@ name: Cleanup Caches for Merged PRs on: pull_request: types: [closed, enqueued] + push: + branches: + - pb/cleanup-cache workflow_dispatch: inputs: pr_number: From a80ab66f2811e399cc8fdf95d86652009ab22f89 Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:49:38 +0100 Subject: [PATCH 4/6] force delete caches for closed pr 4422 Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com> --- .github/workflows/cleanup-cache.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index ba3163dc36..8efaaaf1bd 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -32,11 +32,12 @@ jobs: REPO="${{ github.repository }}" - if [[ "${{ github.event_name }}" == "pull_request" ]]; then - PR_NUMBER="${{ github.event.pull_request.number }}" - else - PR_NUMBER="${{ github.event.inputs.pr_number }}" - fi + PR_NUMBER=4422 + # if [[ "${{ github.event_name }}" == "pull_request" ]]; then + # PR_NUMBER="${{ github.event.pull_request.number }}" + # else + # PR_NUMBER="${{ github.event.inputs.pr_number }}" + # fi BRANCH="refs/pull/$PR_NUMBER/merge" From 05b4508ee04da98a7fbcb310fc865b5737c0a336 Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:50:29 +0100 Subject: [PATCH 5/6] revert testing Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com> --- .github/workflows/cleanup-cache.yml | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index 8efaaaf1bd..9fd846c3f0 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -4,9 +4,6 @@ name: Cleanup Caches for Merged PRs on: pull_request: types: [closed, enqueued] - push: - branches: - - pb/cleanup-cache workflow_dispatch: inputs: pr_number: @@ -32,12 +29,11 @@ jobs: REPO="${{ github.repository }}" - PR_NUMBER=4422 - # if [[ "${{ github.event_name }}" == "pull_request" ]]; then - # PR_NUMBER="${{ github.event.pull_request.number }}" - # else - # PR_NUMBER="${{ github.event.inputs.pr_number }}" - # fi + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + PR_NUMBER="${{ github.event.pull_request.number }}" + else + PR_NUMBER="${{ github.event.inputs.pr_number }}" + fi BRANCH="refs/pull/$PR_NUMBER/merge" From f0c06c745a0e0d8b43b620b7aacc558541f27209 Mon Sep 17 00:00:00 2001 From: pbio <10051819+paulbalaji@users.noreply.github.com> Date: Mon, 9 Sep 2024 12:26:41 +0100 Subject: [PATCH 6/6] closed PRs only Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com> --- .github/workflows/cleanup-cache.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cleanup-cache.yml b/.github/workflows/cleanup-cache.yml index 9fd846c3f0..12b4b740ed 100644 --- a/.github/workflows/cleanup-cache.yml +++ b/.github/workflows/cleanup-cache.yml @@ -3,7 +3,7 @@ name: Cleanup Caches for Merged PRs on: pull_request: - types: [closed, enqueued] + types: [closed] workflow_dispatch: inputs: pr_number: