Skip to content

ci: preserve *-maintenance branches when merging PRs - #243

Merged
whme merged 4 commits into
mainfrom
ci/preserve-maintenance-branches-on-merge
May 23, 2026
Merged

whme merged 4 commits into
mainfrom
ci/preserve-maintenance-branches-on-merge

Conversation

@whme

@whme whme commented May 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Replaces GitHub's repo-wide Automatically delete head branches setting with a workflow that skips <major>.<minor>-maintenance branches (1-3 digit version components, e.g. 0.19-maintenance), so long-lived release branches are not deleted when a cherry-pick PR merges into main.
  • Runs on pull_request_target: closed, only when merged == true and the head and base repo match (forks are skipped since GitHub cannot delete branches in a fork's repo).
  • Treats 422 Reference does not exist as success so re-runs and races with manual deletes stay green; persistent failures surface as a red job.

Important

The repo setting Settings -> General -> Automatically delete head branches must be disabled for this workflow to be the single source of truth. Otherwise the setting wins for non-maintenance branches and races this workflow.

Test plan

  • Disable the repo-wide Automatically delete head branches setting before merging this PR.
  • Merge this PR with a non-maintenance head branch (ci/preserve-maintenance-branches-on-merge) and confirm the workflow runs and the branch is deleted afterwards.
  • Land the next 0.19-maintenance -> main cherry-pick PR and confirm 0.19-maintenance is still present after merge.
  • Confirm that a re-run (or manual delete + workflow re-run) does not fail the job.

GitHub's repo-wide "Automatically delete head branches" setting cannot
exclude long-lived release branches by pattern, so enabling it would
delete branches like `0.19-maintenance` the moment a cherry-pick PR
lands on `main`. Replace it with a workflow that deletes the head
branch on PR merge unless the branch name ends in `-maintenance`.

The repo setting must be disabled for this workflow to be authoritative;
otherwise it races the workflow for non-maintenance branches.

GitHub deletes via REST return 422 when the ref is already gone (manual
delete, prior run, or another workflow); we treat that as success so
re-runs and races stay green. Cross-repo (fork) PRs are skipped because
GitHub cannot delete branches in a fork's repository anyway.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

github-actions Bot commented May 23, 2026 •

Copy link
Copy Markdown

🤖 News Fragment Check

✅ Success! This PR meets the news fragment requirements.

@whme whme added the no-news-fragment-needed Indicates that this PR does not require a news fragment label May 23, 2026
@whme
whme requested a review from Copilot May 23, 2026 19:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Introduces a GitHub Actions workflow to delete merged PR head branches while preserving long-lived *-maintenance branches, replacing GitHub’s repo-wide “Automatically delete head branches” setting.

Changes:

  • Adds a pull_request_target: closed workflow that deletes the head branch after merge when the PR comes from the same repo.
  • Skips deletion for branches whose names end with -maintenance.
  • Makes deletion re-runnable by treating “already deleted” as a successful outcome.

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

Comment thread .github/workflows/delete-merged-branches.yml Outdated
Address review feedback on #243:

- Replace the broad `endsWith('-maintenance')` job filter with a
  precise `^[0-9]{1,3}\.[0-9]{1,3}-maintenance$` check in the script.
  GitHub Actions expressions lack regex support, so the tightened
  check has to live in `run:` instead of the job-level `if:`. This
  prevents an unrelated branch like `foo-maintenance` from being
  spared accidentally.
- Restrict the "already deleted" tolerance to responses that contain
  both `HTTP 422` and `Reference does not exist`, instead of any
  substring `not found`. The previous match swallowed unrelated 404s
  (wrong repo, missing permission, bad API path) and left the merged
  branch undeleted while the workflow reported success.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread .github/workflows/delete-merged-branches.yml Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread .github/workflows/delete-merged-branches.yml
The header comment said this workflow preserves `*-maintenance`
branches, but the active check matches only `<major>.<minor>-maintenance`
with 1-3 digit components. Tighten the wording so the documentation
matches the regex on line 51.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
@whme
whme merged commit 11cafc3 into main May 23, 2026
7 checks passed
@github-actions
github-actions Bot deleted the ci/preserve-maintenance-branches-on-merge branch May 23, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-news-fragment-needed Indicates that this PR does not require a news fragment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants