Skip to content

ci: clean up PR caches on close and shorten artifact retention - #185

Open
sergeyfarin wants to merge 1 commit into
masterfrom
ci/clean-up-pr-caches
Open

sergeyfarin wants to merge 1 commit into
masterfrom
ci/clean-up-pr-caches

Conversation

@sergeyfarin

Copy link
Copy Markdown
Owner

Summary

Stops PR caches and CI artifacts from piling up in Actions storage. A storage audit on 2026-12-05 found one closed PR's Rust cache taking 443 MB and 172 vitest-coverage artifacts (282 MB) going back to September, all kept for the 90-day default.

  • Adds .github/workflows/cleanup-pr-caches.yml: when a pull request closes (merged or not), it deletes the caches saved under refs/pull/N/merge and under the head branch. Only that pull request can restore them, so until now they sat in the 10 GB repository cache quota for up to 7 days and pushed out the default branch's caches. It never touches the default branch's caches, skips pull requests from forks (read-only token), and only warns when a deletion is refused (for example a Dependabot pull request that Dependabot closes itself).
  • pr-tests.yml: the Rust dependency cache becomes restore-only (save-if: false). publish-dist.yml already saves the shared ressim-benchmark-cache key from master, and pull requests restore that copy.
  • retention-days: 7 on vitest-coverage, pr-browser-smoke-diagnostics and pages-smoke-diagnostics, which had no retention set (90-day default). The dist bundle keeps its 14 days.

Testing

  • All changed workflow files parse as YAML; the cleanup step's script passes bash -n.
  • The cleanup workflow runs for the first time when a pull request closes after this merges. Closing this one exercises it.

🤖 Generated with Claude Code

A closed pull request's caches can only be restored by that pull request, so delete them when it closes instead of letting them hold the repository cache quota for up to 7 days.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sergeyfarin

Copy link
Copy Markdown
Owner Author

Review of 9c937fa1

Verdict

Merge after fixes (P3 only). The change is correct and safe. It deletes only pull-request caches and never master's. The cleanup can't be abused: the branch name reaches the script as an environment variable, not pasted into the code. The fixes below are small, but they change behaviour the PR description promises ("when a pull request closes (merged or not)").

Direction and decisions

Cutting storage this way is reasonable. There's no linked issue: the PR has no Closes/Refs line and the commit has no issue number. AGENTS.md wants both, though for CI housekeeping this is a formality.

Findings

P3-1 — .github/workflows/cleanup-pr-caches.yml:9-10, 19-20: conflicted and fork pull requests are never cleaned up.

  • The workflow listens for the pull_request closed event. GitHub doesn't run pull_request workflows while the pull request has a merge conflict. So abandoned conflicted PRs, the most likely ones to be closed unmerged, keep their caches for 7 days.
  • Fork PRs are skipped by the if: on line 20. Its comment says "its branch caches live in the fork". That's true for branch caches, but the fork PR's refs/pull/N/merge caches are stored in this repository and are also left behind.
  • Fix: trigger on pull_request_target: types: [closed] instead.
    • It runs the base branch's copy of the workflow and ignores merge conflicts.
    • It gets a write token for fork PRs too.
    • It's safe here because the job never checks out or runs code from the PR.
    • Keep the same-repository condition only around deleting the head-branch caches.
    • Dependabot-triggered runs stay read-only either way, and the existing warning covers that.

P3-2 — .github/workflows/pr-tests.yml:54-56: save-if: false saves almost nothing and costs rebuild time on the PRs that need it most.

  • In this PR's own run (job 145819074594), the Rust cache was an exact hit: Restored from cache key "v0-rust-ressim-benchmark-cache-Linux-x64-0c0b627b-8e051c22" full match: true. Swatinem/rust-cache never saves after an exact hit, so ordinary PRs already saved nothing.
  • A PR only saves when the key misses: Cargo.lock or Cargo.toml changed, or a new stable Rust release changed the key's environment hash. Those are exactly the multi-push dependency and toolchain PRs. With restore-only, every push of such a PR now rebuilds the changed crates. After a toolchain bump it rebuilds everything, because the fallback key also contains that hash.
  • The quota problem the comment names is already fixed by the new cleanup workflow, which deletes the PR's copy on close.
  • Fix: drop save-if: false and let cleanup-on-close handle the quota, or keep it and state the trade-off in the comment.

P3-3 — .claude/skills/update-dependencies/SKILL.md:88-91: now wrong.
It says "both files under .github/workflows/" and "the Node version is stated in both workflows". There are now three workflow files. The new one has no uses: and no Node version, so the content still holds, but "both" is wrong. Rewording to "every workflow file" would fix it.

Question for the owner

  • Do you accept slower dependency and toolchain PRs in exchange for P3-2's restore-only cache, given that cleanup-on-close already frees the quota?

Noticed outside this PR (candidate follow-up, not filed)

  • master's Rust cache is 265 MB. The deleted PR cache was 443 MB, because PRs also build the native tests, clippy, ressim-py and the compositional tests.
  • PRs restore master's copy with an exact match, so they never save those extra builds. I'd expect every PR run to recompile them from scratch.
  • I couldn't measure how much of the 2.5-minute native-binding step and 1.5-minute compositional step is compilation, because cargo output is quiet in the log. Treat this as a hypothesis.
  • A possible fix is a separate cache key for pr-tests that only master saves.

What I checked and ran, and what I didn't

  • Read all three workflow files in full, plus docs/DEPLOYMENT.md, ressim-validation/ci.md and the update-dependencies skill. Searched the repo for anything that consumes the coverage artifact: nothing does, so 7 days is safe.
  • Listed the live Actions caches. Only three exist, all on refs/heads/master, including the 265 MB Rust key.
  • Parsed all three workflows as YAML: OK.
  • Pulled the cleanup script out and ran bash -n: OK.
  • Dry-ran the script against the real repository with gh cache delete stubbed out, for a feature-branch head and for a master head. The guard works: the master head skips its branch ref, and only refs/pull/185/merge is listed.
  • Confirmed that gh cache list --ref takes the ref formats the script uses.
  • Not run: actionlint and shellcheck (not installed), and the workflow itself on GitHub. Its first real run will be when a PR closes after this merges. No engine gates, since nothing under src/ changed.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
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.

1 participant