Files
lobe-chat/.github/workflows/sync-main-to-canary.yaml
Arvin Xu 93bb83db5d 🔨 chore: improve version sync (#12422)
* update workflow

* update skills

* update skills
2026-02-22 01:09:23 +08:00

130 lines
4.6 KiB
YAML

name: 🔄 Branch Synchronization
on:
workflow_dispatch:
push:
branches:
- main
permissions:
contents: write
pull-requests: write
concurrency:
group: sync-main-to-canary
cancel-in-progress: true
jobs:
sync-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.GH_TOKEN }}
- name: Set up Git
run: |
git config --global user.name 'lobehubbot'
git config --global user.email 'i@lobehub.com'
- name: Sync main to canary
if: github.ref == 'refs/heads/main'
run: |
# Find existing open sync PR by head branch prefix
EXISTING_PR=$(gh pr list --base canary --state open --json number,headRefName \
--jq '[.[] | select(.headRefName | startswith("sync/main-to-canary-"))][0] // empty')
EXISTING_PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number // empty' 2>/dev/null)
# Refresh remote refs to avoid stale comparisons
git fetch origin canary main
# Check if there are actual changes to sync
if [ "$(git rev-parse origin/main)" = "$(git rev-parse origin/canary)" ]; then
echo "No changes to sync"
exit 0
fi
close_stale_pr() {
if [ -n "$EXISTING_PR_NUMBER" ]; then
gh pr close "$EXISTING_PR_NUMBER" --comment "Superseded by $1." --delete-branch || true
fi
}
# 1) Fast-forward: canary is ancestor of main → just move canary pointer
if git merge-base --is-ancestor origin/canary origin/main; then
echo "canary is ancestor of main, fast-forwarding"
git checkout canary
git reset --hard origin/main
if git push origin canary; then
close_stale_pr "fast-forward push"
exit 0
fi
echo "Fast-forward push failed, falling back to merge"
fi
# 2) Merge: canary has unique commits but no conflicts
git checkout canary
git reset --hard origin/canary
if git merge origin/main --no-edit; then
echo "Merge succeeded, pushing directly"
if git push origin canary; then
close_stale_pr "direct merge push"
exit 0
fi
# Direct push failed (e.g. non-fast-forward race), fall back to PR
echo "Direct push failed, falling back to PR"
SYNC_BRANCH="sync/main-to-canary-$(date +'%Y%m%d')-${GITHUB_RUN_ID}"
git checkout -B "$SYNC_BRANCH"
git push origin "$SYNC_BRANCH" -f
gh pr create \
--base canary \
--head "$SYNC_BRANCH" \
--title "🚀 release: sync main branch to canary" \
--body "Automatic sync from main to canary. Direct push failed, please merge this PR." || true
exit 0
fi
# 3) Conflicts: create or update PR for manual resolution
echo "Merge conflicts detected, creating PR"
git merge --abort
if [ -n "$EXISTING_PR_NUMBER" ]; then
gh pr comment "$EXISTING_PR_NUMBER" --body "New commits on \`main\`. Please pull latest \`origin/main\` into this branch to include them."
echo "Commented on existing PR #$EXISTING_PR_NUMBER"
else
SYNC_BRANCH="sync/main-to-canary-$(date +'%Y%m%d')-${GITHUB_RUN_ID}"
git checkout -B "$SYNC_BRANCH" origin/canary
if ! git merge origin/main --no-edit; then
git add -A
git commit --no-edit -m "chore: merge main into canary (has conflicts to resolve)"
fi
git push origin "$SYNC_BRANCH" -f
printf '%s\n' \
'Automatic sync from main to canary. Merge conflicts detected.' \
'' \
'**Resolution steps:**' \
'```bash' \
'git fetch origin' \
"git checkout $SYNC_BRANCH" \
'git merge origin/main' \
'# Resolve conflicts' \
'git add -A && git commit' \
'git push' \
'```' \
'' \
'> Do NOT merge canary into a main-based branch — always merge main INTO the canary-based branch to keep a clean commit graph.' \
> /tmp/pr-body.md
gh pr create \
--base canary \
--head "$SYNC_BRANCH" \
--title "🚀 release: sync main branch to canary" \
--body-file /tmp/pr-body.md
fi
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}