Skip to content

Add workflow to clean up PR branch caches on close#169

Open
Copilot wants to merge 3 commits intomainfrom
copilot/add-cache-cleanup-workflow
Open

Add workflow to clean up PR branch caches on close#169
Copilot wants to merge 3 commits intomainfrom
copilot/add-cache-cleanup-workflow

Conversation

Copy link

Copilot AI commented Mar 16, 2026

  • Create .github/workflows/cleanup-caches.yml workflow that deletes PR-specific caches when a pull request is closed
    • Remove unnecessary actions/checkout step (script only calls gh actions-cache, no workspace needed)
    • Drop contents: read permission (no longer needed without checkout)
    • Narrow set +e scope to only the delete loop — restored with set -e afterwards so errors outside the loop are still surfaced

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Copilot AI changed the title [WIP] Add workflow to clean up unnecessary caches after merge Add workflow to clean up PR branch caches on close Mar 16, 2026
Copilot AI requested a review from BenjaminMichaelis March 16, 2026 16:40
@BenjaminMichaelis BenjaminMichaelis marked this pull request as ready for review March 16, 2026 17:15
Copilot AI review requested due to automatic review settings March 16, 2026 17:15
Copilot AI temporarily deployed to BuildAndUploadImage March 16, 2026 17:15 Inactive
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a GitHub Actions workflow to remove GitHub Actions caches associated with a PR’s merge ref when the PR is closed, preventing accumulation of stale PR-scoped caches in the repository.

Changes:

  • Introduces a new pull_request: closed workflow to list and delete caches under refs/pull/<number>/merge.
  • Installs the actions/gh-actions-cache GH CLI extension at runtime and iterates cache keys for deletion.
  • Applies repository-scoped permissions intended to allow cache deletion (actions: write).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +10 to +18
contents: read

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v6

Comment on lines +26 to +33
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
@BenjaminMichaelis
Copy link
Member

@copilot address any applicable unresolved pr comments.

…anup workflow

Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 16, 2026

@copilot address any applicable unresolved pr comments.

Addressed both unresolved comments in f5054a2:

  1. Removed the actions/checkout step — the script only calls gh actions-cache and never needs the workspace. Dropped contents: read permission as well since it's no longer needed.
  2. Narrowed set +e scopeset +e now only wraps the delete loop, followed immediately by set -e to restore strict error handling. Failures in the setup/listing steps will still surface as errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants