From 75cfc7cdd4b8f83f247211dc7fc5c5cd433bf746 Mon Sep 17 00:00:00 2001 From: Jakob Lichterfeld Date: Thu, 17 Oct 2024 13:12:44 +0200 Subject: [PATCH] ci: cleanup caches when pr is closed --- .github/workflows/cleanup_caches.yml | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 .github/workflows/cleanup_caches.yml diff --git a/.github/workflows/cleanup_caches.yml b/.github/workflows/cleanup_caches.yml new file mode 100644 index 0000000000..316a9e3b35 --- /dev/null +++ b/.github/workflows/cleanup_caches.yml @@ -0,0 +1,49 @@ +name: Cleanup caches by a branch + +on: + pull_request: + types: + - closed + pull_request_target: + types: + - closed + paths: + - "**/*" + - "!.github/**" # Important: Exclude PRs related to .github from auto-run + - "!.github/workflows/**" # Important: Exclude PRs related to .github/workflows from auto-run + - "!.github/actions/**" # Important: Exclude PRs related to .github/actions from auto-run + +permissions: + contents: read + packages: read + +jobs: + check_paths: + uses: ./.github/workflows/check_paths.yml + + cleanup: + name: Delete caches when PR is closed + needs: check_paths + if: needs.check_paths.outputs.githubfolder != 'true' + runs-on: ubuntu-24.04 + + steps: + - name: Cleanup + run: | + gh extension install actions/gh-actions-cache@d88f49218fd18a37f6f63ee4f8716764316a7f7d #v1.0.4 + + 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 }} + REPO: ${{ github.repository }} + BRANCH: refs/pull/${{ github.event.pull_request.number }}/merge \ No newline at end of file