forked from OpenSalamander/salamander
-
Notifications
You must be signed in to change notification settings - Fork 0
114 lines (98 loc) · 4.55 KB
/
sync.yml
File metadata and controls
114 lines (98 loc) · 4.55 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
name: Sync with upstream
on:
schedule:
- cron: '0 3 * * 0' #
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write # Needed to push to your repo
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history for proper syncing
- name: Set up Git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Get parent (upstream) repo full name
id: get-upstream
run: |
repo="${{ github.repository }}"
parent=$(gh api repos/$repo --jq '.parent.full_name // empty' 2>/dev/null)
if [ -z "$parent" ]; then
echo "❌ No upstream repository found. This repository may not be a fork."
echo "💡 This workflow only works with forked repositories."
exit 1
fi
echo "parent=$parent" >> "$GITHUB_OUTPUT"
echo "🔍 Found upstream repository: $parent"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Add upstream and fetch
run: |
git remote add upstream https://github.com/${{ steps.get-upstream.outputs.parent }}.git
git fetch upstream
- name: Get default branch names
id: get-branches
run: |
# Get upstream default branch
upstream_branch=$(git remote show upstream | grep 'HEAD branch' | cut -d' ' -f5)
# Get origin default branch
origin_branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
echo "upstream_branch=$upstream_branch" >> "$GITHUB_OUTPUT"
echo "origin_branch=$origin_branch" >> "$GITHUB_OUTPUT"
echo "🔍 Upstream default branch: $upstream_branch"
echo "🔍 Origin default branch: $origin_branch"
- name: Sync with upstream
run: |
upstream_branch="${{ steps.get-branches.outputs.upstream_branch }}"
origin_branch="${{ steps.get-branches.outputs.origin_branch }}"
# Checkout the appropriate branch
if [ "$upstream_branch" = "$origin_branch" ]; then
git checkout "$origin_branch"
else
echo "⚠️ Branch mismatch: upstream uses '$upstream_branch', origin uses '$origin_branch'"
echo "Syncing upstream/$upstream_branch to origin/$origin_branch"
git checkout "$origin_branch"
fi
# Attempt fast-forward merge
echo "🔄 Attempting to sync with upstream/$upstream_branch..."
if git merge --ff-only upstream/$upstream_branch; then
echo "✅ Successfully synced with upstream"
echo "sync_success=true" >> "$GITHUB_OUTPUT"
else
echo "⚠️ Cannot fast-forward merge. Manual intervention may be required."
echo "This usually means there are local commits that conflict with upstream changes."
echo "sync_success=false" >> "$GITHUB_OUTPUT"
# Show the divergence
echo "📊 Commit comparison:"
git log --oneline --graph --decorate --all -10
# Reset to avoid leaving repo in bad state
git merge --abort 2>/dev/null || true
exit 1
fi
id: sync
- name: Push to origin
if: steps.sync.outputs.sync_success == 'true'
run: |
origin_branch="${{ steps.get-branches.outputs.origin_branch }}"
echo "📤 Pushing changes to origin/$origin_branch..."
git push origin "$origin_branch"
echo "✅ Successfully pushed changes to origin"
- name: Create summary
if: always()
run: |
if [ "${{ steps.sync.outputs.sync_success }}" = "true" ]; then
echo "## ✅ Sync Successful" >> $GITHUB_STEP_SUMMARY
echo "Repository has been successfully synced with upstream." >> $GITHUB_STEP_SUMMARY
else
echo "## ⚠️ Sync Failed" >> $GITHUB_STEP_SUMMARY
echo "Could not sync with upstream. Manual intervention may be required." >> $GITHUB_STEP_SUMMARY
echo "This typically happens when there are conflicting local commits." >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Upstream:** ${{ steps.get-upstream.outputs.parent }}" >> $GITHUB_STEP_SUMMARY
echo "**Timestamp:** $(date -u)" >> $GITHUB_STEP_SUMMARY