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

This commit is contained in:
2026-03-02 14:20:53 +03:00
parent 1880440dee
commit 20c5fd20cc
3 changed files with 83 additions and 60 deletions
+48
View File
@@ -0,0 +1,48 @@
name: Auto Merge
on:
workflow_run:
workflows: ["Code Quality"]
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"
# Only merge PRs from master to main
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
+35
View File
@@ -0,0 +1,35 @@
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: |
# Check if there's already an open PR from master to main
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
-60
View File
@@ -1,60 +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: Setup Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Sync branches
run: |
# Get the branch that was pushed to
PUSHED_BRANCH="${GITHUB_REF#refs/heads/}"
if [ "$PUSHED_BRANCH" = "main" ]; then
TARGET_BRANCH="master"
else
TARGET_BRANCH="main"
fi
echo "Syncing $PUSHED_BRANCH → $TARGET_BRANCH"
# Fetch all branches
git fetch origin
# Check if target branch exists
if git show-ref --verify --quiet refs/remotes/origin/$TARGET_BRANCH; then
# Get commit hashes
PUSHED_COMMIT=$(git rev-parse origin/$PUSHED_BRANCH)
TARGET_COMMIT=$(git rev-parse origin/$TARGET_BRANCH)
if [ "$PUSHED_COMMIT" = "$TARGET_COMMIT" ]; then
echo "Branches are already in sync"
exit 0
fi
echo "Updating $TARGET_BRANCH to match $PUSHED_BRANCH"
git push origin origin/$PUSHED_BRANCH:refs/heads/$TARGET_BRANCH --force
else
echo "Creating $TARGET_BRANCH from $PUSHED_BRANCH"
git push origin origin/$PUSHED_BRANCH:refs/heads/$TARGET_BRANCH
fi
echo "Sync complete!"