Skip to content

Commit 416e29a

Browse files
authored
Merge pull request #112 from ahoppen/auto-merge
Add GitHub action that automatically creates a PR to merge main into a release branch
2 parents 6f3e0c5 + aad5838 commit 416e29a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Create automerge PR
2+
3+
# Merges `head_branch` into `base_branch` and opens a PR to incorporate that merge commit into `base_branch`.
4+
#
5+
# The typical use case for this is in the first period after Swift has cut release branches.
6+
# Some repositories want to include most changes from `main` also in the release branch. When this job is set up, it can automatically create PRs to merge `main` into the release branch.
7+
# Maintainers of the package can then inspect the changes to ensure that they are not too risky for the release branch.
8+
# We will also run the normal PR testing on these changes, ensuring that these modifications don't break the build.
9+
#
10+
# Example usage in a repository:
11+
#
12+
# name: Create PR to merge main into release branch
13+
#
14+
# # In the first period after branching the release branch, we typically want to include all changes from `main` also in the release branch. This workflow automatically creates a PR every Monday to merge main into the release branch.
15+
# # Later in the release cycle we should stop this practice to avoid landing risky changes by disabling this workflow. To do so, disable the workflow as described in https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-workflow-runs/disabling-and-enabling-a-workflow
16+
#
17+
# on:
18+
# schedule:
19+
# - cron: '0 9 * * MON'
20+
# workflow_dispatch:
21+
#
22+
# jobs:
23+
# create_merge_pr:
24+
# name: Create PR to merge main into release branch
25+
# uses: swiftlang/github-workflows/.github/workflows/create_autmerge_pr.yml@main
26+
# if: (github.event_name == 'schedule' && github.repository == 'swiftlang/swift-format') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork
27+
# with:
28+
# base_branch: release/6.2
29+
30+
on:
31+
workflow_call:
32+
inputs:
33+
base_branch:
34+
type: string
35+
description: The branch into which head_branch should be merged
36+
required: true
37+
head_branch:
38+
type: string
39+
description: The branch that should be merged into base_branch
40+
default: main
41+
pr_message:
42+
type: string
43+
description: The message that should be included in the PR created by this job
44+
default: This PR was automatically opened by a GitHub action. Review the changes included in this PR and determine if they should be included in the release branch. If yes, merge the PR. Otherwise revert changes that should not be included on this branch.
45+
46+
jobs:
47+
create_merge_pr:
48+
name: Create PR to merge ${{ inputs.head_branch }} into ${{ inputs.base_branch }} branch
49+
runs-on: ubuntu-latest
50+
steps:
51+
- name: Checkout repository
52+
uses: actions/checkout@v4
53+
with:
54+
fetch-depth: 0
55+
- name: Create merge commit
56+
id: create_merge_commit
57+
run: |
58+
# Without this, we can't perform git operations in GitHub actions.
59+
git config --global --add safe.directory "$(realpath .)"
60+
git config --local user.name 'swift-ci'
61+
git config --local user.email '[email protected]'
62+
63+
git checkout ${{ inputs.base_branch }}
64+
git merge ${{ inputs.head_branch }}
65+
66+
if [[ "$(git rev-parse HEAD)" = "$(git rev-parse ${{ inputs.head_branch }})" ]]; then
67+
echo "has_merged_commits=true" >> "$GITHUB_OUTPUT"
68+
else
69+
echo "has_merged_commits=false" >> "$GITHUB_OUTPUT"
70+
fi
71+
- name: Push branch and create PR
72+
id: push_branch
73+
if: ${{ steps.create_merge_commit.outputs.has_merged_commits == 'true' }}
74+
env:
75+
GH_TOKEN: ${{ github.token }}
76+
run: |
77+
PR_BRANCH="automerge/merge-main-$(date +%Y-%m-%d_%H-%M)"
78+
git checkout -b "$PR_BRANCH"
79+
git push --set-upstream origin "$PR_BRANCH"
80+
81+
gh pr create \
82+
--base "${{ inputs.base_branch }}" \
83+
--head "$PR_BRANCH" \
84+
--title 'Merge `${{ inputs.head_branch }}` into `${{ inputs.base_branch }}`' \
85+
--body '${{ inputs.pr_message }}'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

0 commit comments

Comments
 (0)