ci: org-wide branch cleanup for merged/closed PRs#2
Open
KenanBek wants to merge 1 commit into
Open
Conversation
Adds a nightly workflow (run from this org .github repo) that keeps branch hygiene across all active iomete repos: 1. enforce-delete-on-merge.sh — ensures the native "delete head branch on merge" setting is on for every active repo (covers MERGED PRs instantly and auto-onboards new repos; there is no org-level default for this setting). 2. delete-stale-branches.sh — deletes head branches whose PRs are now CLOSED or MERGED, which GitHub does not do natively. Heavily guarded (skips forks, the default branch, protected/ruleset branches, branches with an open PR, and recently-pushed branches) and dry-run by default. Cross-repo writes use a BRANCH_CLEANUP_TOKEN secret (the default GITHUB_TOKEN cannot act on other repos). Setup + rollout steps are in docs/branch-cleanup-policy.md. Ships in dry-run mode; flip the BRANCH_CLEANUP_DRY_RUN repo variable to go live after reviewing the logs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
An org-wide policy to auto-delete branches for merged and closed PRs across all active
iometerepos.GitHub natively deletes a branch only when a PR is merged — never on close-without-merge, and there's no org-level default. This PR fills both gaps with one nightly job that lives here in the org
.githubrepo and acts across every active repo via the API.Already applied (outside this PR)
delete_branch_on_merge("Automatically delete head branches") has been enabled on all 73 active repos (60 were previously off). Merged-PR branches now auto-delete instantly, org-wide.What this PR adds
.github/workflows/branch-cleanup.ymlscripts/enforce-delete-on-merge.shscripts/delete-stale-branches.shdocs/branch-cleanup-policy.mdSafety
Ships in dry-run (
BRANCH_CLEANUP_DRY_RUNdefaults totrue) — it only logs what it would delete until you flip the variable. A branch is deleted only when all hold: it had a closed/merged PR, head is in this repo (not a fork), it's not the default branch, name isn't protected (main/master/release/*/…), no open PR, not protected by a rule/ruleset, and its tip commit is older thanGRACE_DAYS(default 7). Validated against 5 repos in dry-run: correctly flagged 12 real stale branches, skipped 2.To activate after merge
BRANCH_CLEANUP_TOKENsecret on this repo. (The defaultGITHUB_TOKENcan't act on other repos.)BRANCH_CLEANUP_DRY_RUN=falseto go live. For a one-time historical backfill, run the workflow manually with a largelookback_days.See
docs/branch-cleanup-policy.mdfor details.