forked from imzyb/MiSub
-
Notifications
You must be signed in to change notification settings - Fork 0
78 lines (63 loc) · 2.69 KB
/
Copy pathfork-sync.yml
File metadata and controls
78 lines (63 loc) · 2.69 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
name: Fork Sync
on:
schedule:
- cron: "0 22 * * *" # 每天北京时间 06:00 执行
workflow_dispatch:
permissions:
contents: write
actions: write
jobs:
sync_with_upstream:
name: Sync with Upstream
runs-on: ubuntu-latest
if: ${{ github.event.repository.fork }}
steps:
- name: Checkout fork
uses: actions/checkout@v4
with:
token: ${{ secrets.SYNC_PAT != '' && secrets.SYNC_PAT || secrets.GITHUB_TOKEN }}
fetch-depth: 0
ref: main
- name: Configure Git and remotes
env:
SYNC_PAT: ${{ secrets.SYNC_PAT }}
run: |
set -euo pipefail
git config user.name "GitHub Actions Bot"
git config user.email "github-actions[bot]@users.noreply.github.com"
git checkout main || git checkout -b main
if git remote get-url upstream >/dev/null 2>&1; then
git remote set-url upstream https://github.com/imzyb/MiSub.git
else
git remote add upstream https://github.com/imzyb/MiSub.git
fi
if [ -n "${SYNC_PAT:-}" ]; then
git remote set-url origin "https://x-access-token:${SYNC_PAT}@github.com/${{ github.repository }}.git"
echo "✅ 检测到 SYNC_PAT,将使用 PAT 推送(支持同步 .github/workflows)。"
else
echo "⚠️ 未配置 SYNC_PAT,将使用默认 GITHUB_TOKEN。"
fi
git fetch --prune origin main
git fetch --prune upstream main
- name: Mirror upstream main
env:
SYNC_PAT: ${{ secrets.SYNC_PAT }}
run: |
set -euo pipefail
ORIGIN_SHA="$(git rev-parse origin/main)"
UPSTREAM_SHA="$(git rev-parse upstream/main)"
if [ "${ORIGIN_SHA}" = "${UPSTREAM_SHA}" ]; then
echo "✨ Fork 已与上游完全一致,无需同步。"
exit 0
fi
if [ -z "${SYNC_PAT:-}" ]; then
WORKFLOW_DIFF="$(git diff --name-only "${ORIGIN_SHA}" "${UPSTREAM_SHA}" -- .github/workflows || true)"
if [ -n "${WORKFLOW_DIFF}" ]; then
echo "::notice title=Fork Sync skipped::上游包含 workflow 文件变更,默认 GITHUB_TOKEN 不能创建或更新 workflow 文件。请在 fork 仓库配置 secret:SYNC_PAT(repo + workflow 权限)后手动重新运行,或在网页使用 Sync fork / Discard commits 对齐上游。"
exit 0
fi
fi
echo "🚀 正在镜像上游 main,fork 本地 main 上的额外提交会被丢弃。"
git reset --hard upstream/main
git push --force-with-lease origin main
echo "✅ Fork main 已镜像到上游 main。"