Replace force-push sync with PR-based auto-merge workflow

This commit is contained in:
2026-03-02 14:22:23 +03:00
parent 718fea7cad
commit bc2ecb3a0e
3 changed files with 81 additions and 36 deletions
+47
View File
@@ -0,0 +1,47 @@
name: Auto Merge
on:
workflow_run:
workflows: ["CI"]
types: [completed]
jobs:
auto-merge:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request'
permissions:
contents: write
pull-requests: write
steps:
- name: Find and merge master → main PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
HEAD_BRANCH="${{ github.event.workflow_run.head_branch }}"
echo "Workflow ran on branch: $HEAD_BRANCH"
if [ "$HEAD_BRANCH" != "master" ]; then
echo "Not a master branch PR, skipping"
exit 0
fi
PR_NUMBER=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--base main \
--head master \
--state open \
--json number \
--jq '.[0].number')
if [ -z "$PR_NUMBER" ]; then
echo "No open PR from master to main found, skipping"
exit 0
fi
echo "Merging PR #$PR_NUMBER"
gh pr merge "$PR_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--merge \
--delete-branch=false
+34
View File
@@ -0,0 +1,34 @@
name: Auto PR (master → main)
on:
push:
branches: [master]
jobs:
create-pr:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create or update PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
EXISTING_PR=$(gh pr list --base main --head master --state open --json number --jq '.[0].number')
if [ -n "$EXISTING_PR" ]; then
echo "PR #$EXISTING_PR already exists — new commits will appear automatically"
else
echo "Creating new PR: master → main"
gh pr create \
--base main \
--head master \
--title "Sync: master → main" \
--body "Automated PR to sync master branch changes to main.
This PR was created automatically and will be merged once CI checks pass."
fi
-36
View File
@@ -1,36 +0,0 @@
name: Sync main and master branches
on:
push:
branches:
- main
- master
jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Sync branches
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "main was updated, syncing master..."
git checkout master
git reset --hard origin/main
git push origin master --force
elif [ "${{ github.ref }}" = "refs/heads/master" ]; then
echo "master was updated, syncing main..."
git checkout main
git reset --hard origin/master
git push origin main --force
fi