From ce24e1092304e1fd0e8602f9a0c3e4e9d238875a Mon Sep 17 00:00:00 2001 From: sidey79 <7968127+sidey79@users.noreply.github.com> Date: Sun, 21 Apr 2024 11:11:51 +0200 Subject: [PATCH] feat[build]: remove caches for merges pull requests --- .github/workflows/cacheCleanup.yml | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/cacheCleanup.yml diff --git a/.github/workflows/cacheCleanup.yml b/.github/workflows/cacheCleanup.yml new file mode 100644 index 00000000..7ad33ba3 --- /dev/null +++ b/.github/workflows/cacheCleanup.yml @@ -0,0 +1,36 @@ +name: cleanup caches by a branch / PR +on: + pull_request: + types: + - closed + workflow_dispatch: + +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: 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 | 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 }}