Skip to content

Commit 6ca910e

Browse files
committed
Switches backmerge to tag-based PR with auto-merge
1 parent b799bed commit 6ca910e

1 file changed

Lines changed: 75 additions & 23 deletions

File tree

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
# This workflow creates a backmerge PR from DEFAULT_BRANCH into `develop`
2-
# whenever a new version tag is pushed (v*).
1+
# This workflow creates a backmerge PR into `develop` whenever a new version tag is pushed (v*).
32
#
4-
# Usage:
5-
# - Triggered automatically on tag push (v*).
6-
# - Creates a PR titled "Backmerge: DEFAULT_BRANCH into develop".
7-
# - Adds the label "[maintainer] auto-pull-request" so it is excluded from changelogs.
3+
# Key points:
4+
# - The PR head is a temporary branch that points at the *tag commit* (refs/tags/vX.Y.Z).
5+
# After merging, `develop` can `git describe` as vX.Y.Z-... because the tag commit becomes an ancestor.
6+
# - The PR is auto-merged using a MERGE COMMIT (not squash) via a GitHub App token.
7+
# The GitHub App must be added to the develop ruleset bypass list.
8+
#
9+
# Required repo config:
10+
# - Actions secret: ES_BACKMERGE_PRIVATE_KEY (GitHub App private key PEM)
11+
# - Actions variable: ES_BACKMERGE_APP_ID (GitHub App ID)
812

9-
name: Backmerge PR creation
13+
name: Backmerge PR (tag -> develop)
1014

1115
on:
1216
push:
@@ -17,28 +21,76 @@ permissions:
1721
contents: write
1822
pull-requests: write
1923

20-
# Set the environment variables to be used in all jobs defined in this workflow
21-
env:
22-
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
23-
2424
jobs:
2525
create-backmerge-pr:
2626
runs-on: ubuntu-latest
27+
2728
steps:
28-
- name: Checkout ${{ env.DEFAULT_BRANCH }} branch
29+
- name: Create GitHub App installation token
30+
id: app-token
31+
uses: actions/create-github-app-token@v2
32+
with:
33+
app-id: ${{ vars.ES_BACKMERGE_APP_ID }}
34+
private-key: ${{ secrets.ES_BACKMERGE_PRIVATE_KEY }}
35+
36+
- name: Checkout tag commit
2937
uses: actions/checkout@v5
3038
with:
31-
ref: ${{ env.DEFAULT_BRANCH }}
39+
ref: ${{ github.ref }} # refs/tags/vX.Y.Z
40+
fetch-depth: 0
41+
token: ${{ steps.app-token.outputs.token }}
42+
43+
- name: Create and push backmerge branch at tag
44+
id: vars
45+
run: |
46+
set -euo pipefail
47+
48+
TAG='${{ github.ref_name }}'
49+
BRANCH="backmerge/${TAG}"
50+
51+
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
52+
echo "branch=${BRANCH}" >> "$GITHUB_OUTPUT"
53+
54+
git config user.name "github-actions[bot]"
55+
git config user.email "github-actions[bot]@users.noreply.github.com"
56+
57+
# Create/move branch to point exactly at the tag commit
58+
git checkout -B "$BRANCH"
59+
60+
# Push (force makes re-runs idempotent for the same tag)
61+
git push --force --set-upstream origin "$BRANCH"
62+
env:
63+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
64+
65+
- name:
66+
Create PR from tag backmerge branch to develop (or reuse if exists)
67+
run: |
68+
set -euo pipefail
69+
70+
TITLE="Backmerge: ${{ steps.vars.outputs.tag }} into develop"
71+
72+
BODY="⚠️ This PR is created automatically for backmerge of a new release tag commit \`${{ steps.vars.outputs.tag }}\` into \`develop\`.
73+
74+
It is labeled '[maintainer] auto-pull-request' and is excluded from release notes and version bump logic."
75+
76+
if gh pr view --repo "${{ github.repository }}" --head "${{ steps.vars.outputs.branch }}" >/dev/null 2>&1; then
77+
echo "PR already exists for head=${{ steps.vars.outputs.branch }}"
78+
else
79+
gh pr create \
80+
--repo "${{ github.repository }}" \
81+
--base develop \
82+
--head "${{ steps.vars.outputs.branch }}" \
83+
--title "$TITLE" \
84+
--label "[maintainer] auto-pull-request" \
85+
--body "$BODY"
86+
fi
87+
env:
88+
GH_TOKEN: ${{ steps.app-token.outputs.token }}
3289

33-
- name: Create PR from ${{ env.DEFAULT_BRANCH }} to develop
90+
- name: Enable auto-merge using MERGE COMMIT
3491
run: |
35-
gh pr create \
36-
--base develop \
37-
--head ${{ env.DEFAULT_BRANCH }} \
38-
--title "Backmerge: ${{ env.DEFAULT_BRANCH }} into develop" \
39-
--label "[maintainer] auto-pull-request" \
40-
--body "⚠️ This PR is created automatically for backmerges changes from \`${{ env.DEFAULT_BRANCH }}\` into \`develop\`, following a new release tag push.
41-
42-
It is labeled \`[maintainer] auto-pull-request\` and is excluded from release notes and version bump logic."
92+
set -euo pipefail
93+
# Merge the PR identified by its head branch.
94+
gh pr merge --repo "${{ github.repository }}" --merge --auto "${{ steps.vars.outputs.branch }}"
4395
env:
44-
GITHUB_TOKEN: ${{ secrets.GH_API_PERSONAL_ACCESS_TOKEN }}
96+
GH_TOKEN: ${{ steps.app-token.outputs.token }}

0 commit comments

Comments
 (0)