Skip to content

Commit e524aa6

Browse files
committed
Open merge-back-release PRs from a seprate branch
1 parent 5827925 commit e524aa6

File tree

3 files changed

+90
-36
lines changed

3 files changed

+90
-36
lines changed

.github/workflows/merge-back.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Merge back a reference to main
2+
on:
3+
workflow_call:
4+
inputs:
5+
sha:
6+
required: true
7+
type: string
8+
secrets:
9+
BOT_GITHUB_TOKEN:
10+
required: true
11+
12+
jobs:
13+
merge-back:
14+
name: Merge back the reference to main
15+
runs-on: ubuntu-22.04
16+
17+
steps:
18+
- name: Push branch and open a PR
19+
uses: actions/github-script@v7.0.1
20+
env:
21+
SHA: ${{ inputs.sha }}
22+
with:
23+
github-token: ${{ secrets.BOT_GITHUB_TOKEN }}
24+
script: |
25+
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
26+
const sha = process.env.SHA;
27+
const branch = `ref-merge/${sha}`;
28+
const ref = `heads/${branch}`;
29+
30+
await github.rest.git.createRef({
31+
owner,
32+
repo,
33+
ref,
34+
sha,
35+
});
36+
console.log(`Created branch ${branch} to ${sha}`);
37+
38+
// Create a PR to merge the branch back to main
39+
const pr = await github.rest.pulls.create({
40+
owner,
41+
repo,
42+
head: branch,
43+
base: 'main',
44+
title: `Automatic merge back to main`,
45+
body: `This pull request was automatically created by the release workflow. It merges the release branch back to main.`,
46+
maintainer_can_modify: true,
47+
});
48+
console.log(`Created pull request #${pr.data.number} to merge the release branch back to main`);
49+
console.log(`PR URL: ${pr.data.html_url}`);
50+
51+
// Add the `T-Task` label to the PR
52+
await github.rest.issues.addLabels({
53+
owner,
54+
repo,
55+
issue_number: pr.data.number,
56+
labels: ['T-Task'],
57+
});
58+
59+
// Enable auto-merge on the PR
60+
await github.graphql(
61+
`
62+
mutation AutoMerge($id: ID!) {
63+
enablePullRequestAutoMerge(input: {
64+
pullRequestId: $id,
65+
mergeMethod: MERGE,
66+
}) {
67+
clientMutationId
68+
}
69+
}
70+
`,
71+
{ id: pr.data.node_id },
72+
);

.github/workflows/release-branch.yaml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ jobs:
7373
secrets:
7474
BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
7575

76+
merge-back:
77+
uses: ./.github/workflows/merge-back.yaml
78+
needs: [tag]
79+
with:
80+
sha: ${{ needs.tag.outputs.sha }}
81+
secrets:
82+
BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
83+
7684
branch:
7785
name: Create a new release branch
7886
runs-on: ubuntu-22.04
@@ -103,20 +111,3 @@ jobs:
103111
sha,
104112
});
105113
console.log(`Created branch ${branch} from ${sha}`);
106-
107-
- name: Checkout the code
108-
uses: actions/checkout@v4.2.2
109-
with:
110-
ref: "release/v${{ needs.compute-version.outputs.short }}"
111-
112-
- name: Open a pull request to merge the branch into main
113-
env:
114-
VERSION: ${{ needs.compute-version.outputs.short }}
115-
GH_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
116-
run: |
117-
gh pr create \
118-
--title "Release branch $VERSION" \
119-
--body "This pull request was automatically created by the release workflow. It merges the release branch back to main." \
120-
--base main \
121-
--head "release/v$VERSION" \
122-
--label "T-Task"

.github/workflows/release-bump.yaml

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
- name: Compute the new minor RC
4343
id: next
4444
env:
45-
BUMP: ${{ github.event.inputs.rc && 'prerelease' || 'patch' }}
45+
BUMP: ${{ inputs.rc && 'prerelease' || 'patch' }}
4646
VERSION: ${{ steps.current.outputs.version }}
4747
run: echo "version=$(npx --yes semver@7.5.4 -i "$BUMP" --preid rc "$VERSION")" >> "$GITHUB_OUTPUT"
4848

@@ -54,6 +54,15 @@ jobs:
5454
secrets:
5555
BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
5656

57+
merge-back:
58+
uses: ./.github/workflows/merge-back.yaml
59+
needs: [tag]
60+
if: inputs.merge-back
61+
with:
62+
sha: ${{ needs.tag.outputs.sha }}
63+
secrets:
64+
BOT_GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
65+
5766
update-branch:
5867
name: Update the release branch
5968
runs-on: ubuntu-22.04
@@ -83,21 +92,3 @@ jobs:
8392
sha,
8493
});
8594
console.log(`Updated branch ${branch} to ${sha}`);
86-
87-
- name: Checkout the code
88-
uses: actions/checkout@v4.2.2
89-
with:
90-
ref: "${{ github.ref_name }}"
91-
92-
- name: Open a pull request to merge the release branch back to main
93-
if: github.event.inputs.merge-back
94-
env:
95-
VERSION: ${{ needs.compute-version.outputs.version }}
96-
GH_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
97-
run: |
98-
gh pr create \
99-
--title "Release branch $VERSION" \
100-
--body "This pull request was automatically created by the release workflow. It merges the release branch back to main." \
101-
--base main \
102-
--head "$GITHUB_REF_NAME" \
103-
--label "T-Task"

0 commit comments

Comments
 (0)