Skip to content

[FEATURE] Add workflow to cleanup per-PR GitHub Actions caches on close #1155

@mr-raj12

Description

@mr-raj12

Why do we need this improvement?

GitHub Actions caches accumulate per branch and per pull request. On every PR run, caches are written scoped to refs/pull/<n>/merge (via setup-node caches in the centrally managed workflows, and any additional test/build caches). When a PR is merged or closed, those caches are not removed automatically — they keep counting against the repository's 10 GB cache quota until GitHub's 7-day LRU eviction kicks in.

Under the quota, the LRU policy can evict live caches for master and for currently open PRs before it reaches the stale per-PR entries. The result is unnecessary cache misses on the branches that matter most, which increases CI time for everyone.

How will this change help?

A small maintenance workflow triggered on pull_request (type: closed) would list the caches scoped to the closing PR's ref and delete them via the GitHub REST API. Net effect:

  • Cache quota freed sooner → fewer cold builds on master and on open PRs.
  • No behavioral change to existing CI jobs — the new workflow runs only after a PR has closed.
  • No changes to any existing workflow file, so no conflict with the # This action is centrally managed in https://github.com/asyncapi/.github/ header carried by every current workflow in this repo.

Screenshots

No response

How could it be implemented/designed?

Add a new file .github/workflows/cleanup-pr-caches.yml:

name: Cleanup caches for closed PRs

on:
  pull_request:
    types: [closed]

permissions:
  actions: write
  contents: read

jobs:
  cleanup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v7
        with:
          script: |
            const ref = `refs/pull/${context.payload.pull_request.number}/merge`;
            const { data } = await github.rest.actions.getActionsCacheList({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref,
            });
            for (const cache of data.actions_caches) {
              await github.rest.actions.deleteActionsCacheById({
                owner: context.repo.owner,
                repo: context.repo.repo,
                cache_id: cache.id,
              });
            }

Notes:

  • New file, so it is not in scope of the central asyncapi/.github sync that overwrites existing workflows here.
  • Scoped permissions (actions: write, contents: read) — no write access to repo contents.
  • If the org prefers pinning actions to immutable SHAs, actions/github-script@v7 can be pinned accordingly.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions