-
Notifications
You must be signed in to change notification settings - Fork 2
56 lines (48 loc) · 1.92 KB
/
sync-branches.yml
File metadata and controls
56 lines (48 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
name: Sync Branches with Main
on:
push:
branches: [main]
permissions:
contents: write
jobs:
sync:
runs-on: ubuntu-latest
strategy:
matrix:
branch: [content] # Add new branches here
steps:
- name: Checkout Main Branch
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Git User
run: |
git config user.name "GitHub Action"
git config user.email "action@github.com"
- name: Sync with ${{ matrix.branch }}
run: |
TARGET_BRANCH="${{ matrix.branch }}"
git fetch --all --prune
# Create local branch tracking origin/${TARGET_BRANCH}
git checkout -b ${TARGET_BRANCH} origin/${TARGET_BRANCH} || git checkout ${TARGET_BRANCH}
# Check if target branch is an ancestor of main (can fast-forward)
if git merge-base --is-ancestor HEAD origin/main 2>/dev/null; then
echo "Branch ${TARGET_BRANCH} is behind main and can be fast-forwarded"
git reset --hard origin/main
git push origin ${TARGET_BRANCH} --force
else
echo "Branch ${TARGET_BRANCH} has diverged from main, attempting merge"
# Merge origin/main into the target branch
# Use --no-ff to create an explicit merge commit when required and
# --no-edit to avoid interactive editor for the merge message.
if git merge --no-ff --no-edit origin/main; then
git push origin ${TARGET_BRANCH}
else
# On merge conflict, abort and push a backup branch so nothing is lost.
git merge --abort || true
BACKUP_BRANCH="backup/${TARGET_BRANCH}-merge-failed-$(date +%s)"
git push origin HEAD:refs/heads/${BACKUP_BRANCH}
echo "Merge failed. Backup pushed to ${BACKUP_BRANCH}. Exiting with failure."
exit 1
fi
fi