From 0d5975fa8018f169b9d285e31ad5bf8257cf2eba Mon Sep 17 00:00:00 2001 From: Tom French Date: Wed, 16 Aug 2023 14:08:56 +0100 Subject: [PATCH 1/4] chore(ci): automatically delete cache entries associated with closed PRs --- .github/workflows/cache-cleanup.yml | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/cache-cleanup.yml diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml new file mode 100644 index 00000000000..c41506e1291 --- /dev/null +++ b/.github/workflows/cache-cleanup.yml @@ -0,0 +1,39 @@ +# This workflow cleans up any cache entries associated with a pull request once it has been closed. +# This prevents us from having many refs/pull/PR_NUMBER/merge cache entries which will never be used. +# +# Note that this will affect both PRs being closed with and without being merged. + +name: Cleanup closed PR cache entries + +on: + pull_request: + types: + - closed + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + + - name: Cleanup + run: | + gh extension install actions/gh-actions-cache + + 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 -L 100 | 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" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 8b9f78541c32a0eb604ee31854ac46b70f6e217c Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 21 Aug 2023 20:23:21 +0100 Subject: [PATCH 2/4] chore: address review comments --- .github/workflows/cache-cleanup.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index c41506e1291..9bf5c9cf293 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -14,16 +14,10 @@ jobs: cleanup: runs-on: ubuntu-latest steps: - - name: Check out code - uses: actions/checkout@v3 - - name: Cleanup run: | gh extension install actions/gh-actions-cache - 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 -L 100 | cut -f 1 ) @@ -37,3 +31,5 @@ jobs: echo "Done" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: "refs/pull/${{ github.event.pull_request.number }}/merge" From d269f9eabdbca332b4de0bf010ae2046a8ef7c66 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 21 Aug 2023 20:25:42 +0100 Subject: [PATCH 3/4] chore: wrap `BRANCH` in an expression --- .github/workflows/cache-cleanup.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index 9bf5c9cf293..3d6951e0c88 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -32,4 +32,4 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} REPO: ${{ github.repository }} - BRANCH: "refs/pull/${{ github.event.pull_request.number }}/merge" + BRANCH: ${{ "refs/pull/${{ github.event.pull_request.number }}/merge" }} From a8b024b53b8cbb2c9cdec101c2bddc65955b3236 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 21 Aug 2023 20:32:07 +0100 Subject: [PATCH 4/4] chore: remove outer braces --- .github/workflows/cache-cleanup.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cache-cleanup.yml b/.github/workflows/cache-cleanup.yml index 3d6951e0c88..cf2b0ec413e 100644 --- a/.github/workflows/cache-cleanup.yml +++ b/.github/workflows/cache-cleanup.yml @@ -32,4 +32,4 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} REPO: ${{ github.repository }} - BRANCH: ${{ "refs/pull/${{ github.event.pull_request.number }}/merge" }} + BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge