Skip to content

Commit f34d26e

Browse files
Add GitHub Actions workflow to clean up PR branch caches on close (#931)
PR merge ref caches (`refs/pull/{number}/merge`) accumulate after PRs close and are never automatically purged, consuming repository cache quota unnecessarily. ## Changes - **`.github/workflows/CleanupCaches.yml`**: Workflow triggered on `pull_request: closed` that enumerates and deletes all Actions caches scoped to the closed PR's merge ref using native `gh cache` CLI commands with `secrets.GITHUB_TOKEN` — no additional secrets required. Uses a `permissions: actions: write` block to follow least-privilege security practice. No repository checkout is required since the script only calls the GitHub API. <!-- START COPILOT CODING AGENT TIPS --> --- 📍 Connect Copilot coding agent with [Jira](https://gh.io/cca-jira-docs), [Azure Boards](https://gh.io/cca-azure-boards-docs) or [Linear](https://gh.io/cca-linear-docs) to delegate work to Copilot in one click without leaving your project management tool. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
1 parent e49c5c0 commit f34d26e

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

.github/workflows/CleanupCaches.yml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@ on:
44
types:
55
- closed
66

7+
permissions:
8+
actions: write
9+
710
jobs:
811
cleanup:
912
runs-on: ubuntu-latest
1013
steps:
11-
- name: Check out code
12-
uses: actions/checkout@v6
13-
1414
- name: Cleanup
1515
run: |
16-
gh extension install actions/gh-actions-cache
17-
1816
REPO=${{ github.repository }}
1917
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
2018
21-
echo "Fetching list of cache key"
22-
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )
19+
echo "Fetching list of cache keys"
20+
cacheKeysForPR=$(gh cache list -R $REPO --ref $BRANCH | cut -f 1)
2321
24-
## Setting this to not fail the workflow while deleting cache keys.
22+
## Setting this to not fail the workflow while deleting cache keys.
2523
set +e
2624
echo "Deleting caches..."
2725
for cacheKey in $cacheKeysForPR
2826
do
29-
gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
27+
gh cache delete $cacheKey -R $REPO
3028
done
3129
echo "Done"
3230
env:

0 commit comments

Comments
 (0)