Repository navigation
ci: preserve *-maintenance branches when merging PRs #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf4a175
ci: preserve *-maintenance branches when merging PRs
whme d95faa7
ci: tighten maintenance-branch guard and delete-tolerance match
whme cefc899
Merge branch 'main' into ci/preserve-maintenance-branches-on-merge
whme ce38a9e
ci: align doc comment with maintenance-branch regex
whme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # | ||
| # Delete Merged Branches | ||
| # | ||
| # Deletes the head branch of a merged pull request, except for branches | ||
| # matching `<major>.<minor>-maintenance` (1-3 digit version components, | ||
| # e.g. `0.19-maintenance`). This replaces GitHub's repo-wide | ||
| # "Automatically delete head branches" setting, which cannot exclude | ||
| # long-lived release branches that we cherry-pick fixes onto and keep | ||
| # around. | ||
| # | ||
| # Repo setting "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. | ||
| # | ||
| # Guards: | ||
| # - PR must be merged (not just closed) | ||
| # - head and base repo must be the same (skip cross-repo forks; GitHub | ||
| # cannot delete fork branches anyway) | ||
| # - head branch must not match `<major>.<minor>-maintenance` (e.g. | ||
| # `0.19-maintenance`), the only naming convention we use for | ||
| # long-lived release branches | ||
| # | ||
| # The delete call is tolerant: if the branch is already gone (manual | ||
| # delete, or another workflow beat us to it) the job still succeeds. | ||
|
|
||
| name: Delete Merged Branches | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [closed] | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| delete-branch: | ||
| if: >- | ||
| github.event.pull_request.merged == true && | ||
| github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Delete head branch | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| REPO: ${{ github.repository }} | ||
| BRANCH: ${{ github.event.pull_request.head.ref }} | ||
| run: | | ||
| # Maintenance branches use `<major>.<minor>-maintenance` with | ||
| # 1-3 digit version components (e.g. `0.19-maintenance`). GitHub | ||
| # Actions expressions cannot express this, so the precise check | ||
| # lives here instead of in the job-level `if:`. | ||
| if [[ "$BRANCH" =~ ^[0-9]{1,3}\.[0-9]{1,3}-maintenance$ ]]; then | ||
| echo "::notice::Preserving maintenance branch $BRANCH." | ||
| exit 0 | ||
| fi | ||
| set +e | ||
| output=$(gh api -X DELETE "repos/$REPO/git/refs/heads/$BRANCH" 2>&1) | ||
| status=$? | ||
| set -e | ||
| printf '%s\n' "$output" | ||
| if [ "$status" -eq 0 ]; then | ||
| echo "::notice::Deleted head branch $BRANCH." | ||
| exit 0 | ||
| fi | ||
| # GitHub returns HTTP 422 "Reference does not exist" when the | ||
| # branch is already gone - treat that exact response as success | ||
| # so re-runs and races stay green. Other failures (404 from a | ||
| # wrong repo/permission, 5xx, etc.) must still fail the job. | ||
| if printf '%s' "$output" | grep -q 'HTTP 422' \ | ||
| && printf '%s' "$output" | grep -qi 'Reference does not exist'; then | ||
| echo "::notice::Head branch $BRANCH was already deleted." | ||
| exit 0 | ||
| fi | ||
| echo "::error::Failed to delete head branch $BRANCH - see log above." | ||
| exit 1 | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.