forked from facebookresearch/detectron2
-
Notifications
You must be signed in to change notification settings - Fork 0
101 lines (86 loc) · 3.73 KB
/
backup.yml
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
name: Backup Fork and Sync
on:
schedule:
- cron: '0 0 * * 0' # 每週日午夜執行
workflow_dispatch: # 允許手動觸發
env:
UPSTREAM_REPO: ${{ vars.UPSTREAM_REPO || 'https://github.com/facebookresearch/detectron2.git' }}
MAIN_BRANCH: ${{ vars.MAIN_BRANCH || 'main' }}
jobs:
backup-and-sync:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN }}
- name: Configure Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Tag current state
run: |
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
git tag -a "pre-sync-$TIMESTAMP" -m "Pre-sync state on $TIMESTAMP"
git push origin "pre-sync-$TIMESTAMP"
- name: Add upstream repository
run: |
git remote add upstream ${{ env.UPSTREAM_REPO }} || git remote set-url upstream ${{ env.UPSTREAM_REPO }}
git fetch --all --tags
- name: Sync and handle conflicts
run: |
branches=$(git branch -r | grep 'origin/' | grep -v 'origin/HEAD' | sed 's/origin\///')
for branch in $branches; do
echo "Processing branch: $branch"
if git ls-remote --exit-code --heads upstream $branch > /dev/null 2>&1; then
git checkout -B $branch origin/$branch
echo "Changes in upstream $branch:"
git log --oneline $branch..upstream/$branch
if git merge upstream/$branch --no-edit --allow-unrelated-histories; then
echo "Successfully merged changes for $branch"
else
echo "Merge conflict in $branch. Creating a new branch with upstream changes."
git merge --abort
conflict_branch="${branch}-upstream-changes"
git checkout -b $conflict_branch upstream/$branch
echo "Created new branch $conflict_branch with upstream changes"
git push origin $conflict_branch
echo "Please manually review and merge changes from $conflict_branch into $branch"
git checkout $branch
fi
git push origin $branch || echo "Failed to push $branch, please check and push manually if needed"
else
echo "Branch $branch does not exist in upstream. Skipping."
fi
done
- name: Create post-sync version tag
run: |
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
git tag -a "post-sync-$TIMESTAMP" -m "Post-sync state on $TIMESTAMP"
git push origin "post-sync-$TIMESTAMP"
- name: Generate sync report
if: always()
run: |
echo "Sync Report" > sync_report.md
echo "===========" >> sync_report.md
echo "" >> sync_report.md
git branch -r | grep 'origin/' | grep -v 'origin/HEAD' | sed 's/origin\///' | while read branch; do
echo "## Branch: $branch" >> sync_report.md
if git log HEAD..origin/$branch --oneline | grep -q .; then
echo "Status: Changes synced" >> sync_report.md
echo "Changes:" >> sync_report.md
git log HEAD..origin/$branch --oneline >> sync_report.md
else
echo "Status: No changes or sync failed" >> sync_report.md
fi
echo "" >> sync_report.md
done
- name: Upload sync report
if: always()
uses: actions/upload-artifact@v3
with:
name: sync-report
path: sync_report.md