diff --git a/.github/workflows/cleanup-pr-caches.yml b/.github/workflows/cleanup-pr-caches.yml new file mode 100644 index 000000000..a22688ec0 --- /dev/null +++ b/.github/workflows/cleanup-pr-caches.yml @@ -0,0 +1,37 @@ +name: Cleanup caches for closed PRs + +on: + pull_request: + types: + - closed + +permissions: + actions: write + contents: read + +jobs: + cleanup: + name: Delete caches for the closed PR + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const ref = `refs/pull/${context.payload.pull_request.number}/merge`; + console.log(`Listing caches for ref: ${ref}`); + const { data } = await github.rest.actions.getActionsCacheList({ + owner: context.repo.owner, + repo: context.repo.repo, + ref, + }); + console.log(`Found ${data.actions_caches.length} cache(s) to delete.`); + for (const cache of data.actions_caches) { + console.log(`Deleting cache id=${cache.id} key=${cache.key}`); + await github.rest.actions.deleteActionsCacheById({ + owner: context.repo.owner, + repo: context.repo.repo, + cache_id: cache.id, + }); + } + console.log('Cleanup finished.');