Repository navigation
342 lines (303 loc) · 17.7 KB
/
Copy pathupdate-source.yml
File metadata and controls
342 lines (303 loc) · 17.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# ==============================================================================
# SideStore 源文件 apps.json 的自动更新(GitHub Actions 版)
#
# ── 为什么要有这一份 ──────────────────────────────────────────────────────
# 原方案把自动更新放在 cnb.cool 的云原生构建里。问题是 CNB 免费额度只有
# 160 核时/月,而且构建开始前要「预冻结」5 分钟(0.08 核时)——额度见底时
# 所有构建直接失败。2026-09 就被 laincat/Rules 一个仓库把额度烧光了。
#
# GitHub Actions 在公开仓库上**不计分钟数**,且原生直连 GitHub Release
# (不需要任何镜像代理),是目前最省心的执行端。
#
# ── 两种用法,靠 secret 是否存在自动切换 ─────────────────────────────────
# 1) 只更新本仓库(默认)——源地址用 GitHub 侧的直链
# 2) 再同步回 cnb.cool(可选)——额外配 CNB_TOKEN / CNB_USER 两个 secrets,
# cnb.cool 那份就变成纯托管镜像,国内访问更快
#
# ── 触发方式:为什么是「高频轮询」而不是「上游发版就触发」───────────────
# 理想做法是订阅那 6 个上游仓库的 release 事件,一发版就同步。但 GitHub
# 的 release 事件**只在事件发生的那个仓库里触发工作流**,跨仓库收不到 ——
# 除非上游主动往我们的 API 打 repository_dispatch,而给别人的仓库配 webhook
# 是我们做不到的(得有那个仓库的管理权限)。
#
# 所以实际可行的是**短间隔轮询**。官方约束(docs.github.com“Events that
# trigger workflows”):
# · 定时任务最短间隔是 **5 分钟**
# · 负载高时会延迟,甚至**丢弃排队中的任务**;整点前后负载最高,
# 官方建议「避开整点」→ 本文件用 7,22,37,52 就是刻意错开整点
# · 定时任务只在**默认分支**上生效
#
# 本任务单次实测仅 **12 秒**(生成 5s + 提交 2s + 固定开销 5s),且公开仓库
# 的 Actions 分钟数免费不限量 → 15 分钟一轮约 96 次/天、总计约 20 分钟/天,
# 成本可忽略。轮询还有个好处:**偶尔被丢弃一轮完全无所谓**,下一轮会补上。
#
# ── 配额/成本 ────────────────────────────────────────────────────────────
# 公开仓库的 Actions 分钟数免费且不限量。
# 15 分钟一轮 + 手动触发,对 GitHub 侧无任何成本。
#
# ⚠️ 实测补充(2026-09-16):cron 是 best-effort,而且「越密越丢」
# 拿三个仍在活跃使用 cron 的公开仓库做的对照:
# · DIYgod/RSSHub 每天 1 次 → 连续 10 天 100%,迟到 +5~6 分
# · home-assistant/core 每小时 → 基本不漏,但迟到 +11~15 分
# · NixOS/nixpkgs 每 10 分钟 → 193 分钟里只跑了 10 次(应约 19 次),约 53%
# 结论:**密 cron 不是「更快」,是「更多次抽奖」。** 丢槽不累积(下一槽会补上),
# 所以轮询场景下反而该跑密一点 —— 前提是单次任务足够轻(本任务实测约 12 秒)。
#
# ⚠️ 另一个坑:**新建的工作流,cron 要先「注册」**。GitHub 需要 15 分钟到
# 1 小时以上才会认领一个新的 schedule;在第一次 schedule 运行真正发生之前,
# Actions 页面上只显示 "This workflow has a workflow_dispatch event trigger",
# 看起来就像 schedule 根本没被解析。所以刚上线时「一个定时都没跑」是正常的,
# 别急着改配置。判断方法(**别盯 UI** —— Actions 页面正文是客户端渲染的,
# curl 到的 HTML 里没有那句话):
# curl -s -H "Authorization: Bearer $TOKEN" \
# "https://api.github.com/repos/laincat/AltStore/actions/runs?event=schedule"
# 返回 total_count = 0 就是还没被认领过。
#
# ── 想做到「秒级」怎么办 ─────────────────────────────────────────────────
# 见下面 repository_dispatch 那段的注释。
# ==============================================================================
name: 更新 SideStore 源
on:
# 每小时一次,在每小时第 7 分钟运行。
# Actions 的 cron 按 UTC 解释;轮询场景下时区无意义。
schedule:
- cron: "7 * * * *"
# 手动触发(改了脚本或想立刻刷新时用)
workflow_dispatch:
# 给外部事件源留的入口:任何拿到本仓库 token 的服务都可以 POST
# https://api.github.com/repos/laincat/AltStore/dispatches
# {"event_type":"upstream-release"}
# 打过来立刻跑一轮,不受 15 分钟间隔限制(想做真正的「发版即同步」时,
# 可以用 Cloudflare Workers 的 1 分钟定时之类,去轮询上游 release 再打这个接口)。
repository_dispatch:
types: [upstream-release]
# 只有"会改变生成结果"的改动才触发,避免改 README 也跑一轮
push:
branches: [main]
paths:
- "tools/**"
- ".github/workflows/update-source.yml"
# 要把生成结果提交回本仓库,必须有写权限
permissions:
contents: write
# 定时与手动触发可能撞车,串行化;不取消进行中的,避免推到一半被中断
concurrency:
group: update-source
cancel-in-progress: false
jobs:
update:
runs-on: ubuntu-latest
timeout-minutes: 20
env:
# 未配置时为:空字符串 → 下面的同步步骤会自动跳过
CNB_REPO: laincat/AltStore
steps:
- name: 取出仓库
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# 需要能 push 回去
persist-credentials: false
fetch-depth: 0
- name: 准备 Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.13"
- name: Same-SHA offline regression gate
run: |
python -m unittest discover -s tests -v
python -m unittest discover -s .github/scripts
python -m compileall -q tools tests .github/scripts
python tools/verify_source.py --min-apps 30
- name: 重新生成源文件
env:
GH_TOKEN: ${{ github.token }}
run: |
set -e
# --refresh:重新问 GitHub 要最新 Release 列表;
# IPA 元数据仍走 cache/probes.json(已探过的版本不重复下载)
python tools/build_source.py --refresh
- name: 校验(不通过就整轮中止)
run: |
set -e
# 某个 app 的 releases 变成空数组时 SideStore 解码会整体抛错,
# 一个坏条目能让整个源加载不了,而这种情况肉眼看不出来 —— 必须卡住。
#
# --min-apps 取「等于当前应用数」:少一个就整轮中止,宁可这轮不提交、
# 15 分钟后重试,也不要把「应用数变少」的源推给用户 —— 那等于用户端
# 凭空少一个 App。生成脚本本身在抓取失败时会沿用上一次的条目,
# 所以正常情况下这个下限永远达标,真触发了一定是有问题。
# ⚠️ 每加/删一个应用,这里都要跟着改。
python tools/verify_source.py --min-apps 30
- name: 更新 README 应用清单与更新记录
run: |
set -e
# 表格按 apps.json 重新生成;有版本变化时把本轮变化追加到 docs/Changelog.md。
# 预发布复核表同样按 cache/releases.json 重新生成。
# 没有变化时这两个文件不会改动,不会产生空提交。
python tools/render_docs.py
- name: 核对两个客户端的解析结果
run: |
set -e
# 同一份 apps.json,两边读的字段完全不同:
# SideStore / AltStore → releaseChannels 里的轨道(beta 要用户手动开开关)
# LiveContainer → 只读扁平 versions,且只取 [0],没有开关也没有版本选择器
# EhPanda 的测试版就是靠「扁平 versions[0]」送到 LiveContainer 的,
# 一旦哪天生成逻辑改动把它挪走了,这里要能看出来。
python tools/simulate_client.py --client sidestore
python tools/simulate_client.py --client livecontainer
- name: 提交到本仓库
id: commit
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add apps.json cache/*.json README.md docs/Changelog.md docs/nightly.md
if git diff --cached --quiet; then
echo "上游无新版本,无变更"
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
# 同一 SHA 上的两次运行(例如 push 与手动触发)会串行排队,但后者的 checkout
# 仍停在旧提交;直接推送会被 non-fast-forward 拒绝。这里先确认远端没前进,
# 前进了就跳过本轮 —— 生成的源下一轮会重新算,不会丢数据。
git fetch --quiet origin main
if [ "$(git rev-parse origin/main)" != "$(git rev-parse HEAD)" ]; then
echo "远端 main 已前进,本轮结果基于旧提交,跳过推送(下一轮重新生成)"
echo "changed=false" >> "$GITHUB_OUTPUT"
git reset --hard --quiet
exit 0
fi
git commit -m "chore(source): 自动更新 $(date -u '+%Y-%m-%d')"
git -c credential.helper= -c 'credential.helper=!gh auth git-credential' push
echo "changed=true" >> "$GITHUB_OUTPUT"
- name: 发布/滚动更新 Release(固定 tag,永远只留一个)
id: release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
TAG="latest"
# ── 为什么是「固定 tag + 滚动覆盖」,而不是「一次内容一个 tag」──────
# 本流水线每小时跑一轮。若每轮都建 tag/release,就是一天 96 个、
# 一周 672 个 —— 另一个仓库踩过同款坑:tag 用构建时间戳,一次清掉
# 1409 个。这里 tag 名永远叫 latest,只覆盖它下面的 asset,于是:
# · URL 永久稳定:/releases/download/latest/apps.json
# · tag / release 数量恒定为 1,不随时间膨胀
# ─────────────────────────────────────────────────────────────
exists=false
if gh release view "$TAG" >/dev/null 2>&1; then exists=true; fi
# ── 判据是「附件内容与当前 apps.json 是否一致」,不是「本轮有没有提交」──
# 这里踩过一个坑(2026-09-18 实测):push 触发的运行里,apps.json 本身
# 就已经是新内容,「提交到本仓库」那步自然报 changed=false —— 于是这里
# 直接跳过,结果 raw 与 cnb.cool 两份都更新了,唯独 Release 附件停在旧
# 版本上,而且这一步还显示 success,从状态里完全看不出来。
# 改成 sha256 比对后就没这个问题:不管变化来自本轮的自动提交,还是来自
# 触发这次运行的 push 本身,都能正确识别。
# 拿不到 digest 时 have 为空 → 判定为「需要上传」,失败方向是安全的。
#
# 首次(release 还不存在)即使内容一致也要建一次,否则用户会以为没生效。
want=$(sha256sum apps.json | awk '{print $1}')
have=""
if [ "$exists" = "true" ]; then
have=$(gh api "repos/${GITHUB_REPOSITORY}/releases/tags/${TAG}" \
--jq '.assets[] | select(.name=="apps.json") | (.digest // "")' \
2>/dev/null | sed 's|^sha256:||' || true)
fi
if [ "$exists" = "true" ] && [ "$have" = "$want" ]; then
echo "附件内容与 apps.json 一致(sha256 ${want:0:16}…)→ 跳过"
echo "url=https://github.com/${GITHUB_REPOSITORY}/releases/download/$TAG/apps.json" \
>> "$GITHUB_OUTPUT"
exit 0
fi
# ⚠️ 用 git rev-parse HEAD,不要用 $GITHUB_SHA:
# schedule 触发时 GITHUB_SHA 是「触发那一刻」的提交,而上面刚产生
# 了新提交(自动更新),两者不是同一个。用错会让 tag 指向旧树。
SHA=$(git rev-parse HEAD)
git tag -f "$TAG" "$SHA"
# tag push 不会触发本工作流(触发器限定了 branches: [main],tag 不匹配)
git -c credential.helper= -c 'credential.helper=!gh auth git-credential' push -f origin "refs/tags/$TAG"
if [ "$exists" = "true" ]; then
gh release upload "$TAG" apps.json --clobber
echo "已滚动覆盖 asset:apps.json(tag ${TAG} -> ${SHA:0:7})"
else
cat > /tmp/release-notes.md <<'EOF'
本 release 的 tag 固定为 `latest`,其下的 `apps.json` 在每次内容变化后被覆盖。
也就是说本页永远只有这一个 release,tag 指向当前内容所在的那个提交。
一个永久稳定的备用下载地址:
```
https://github.com/laincat/AltStore/releases/download/latest/apps.json
```
注意:订阅这个源并不需要走 Release —— `raw.githubusercontent.com` 的直链
一直是可用的。这里主要提供「点击下载」入口与内容快照。
EOF
gh release create "$TAG" apps.json \
--title "最新源文件(滚动更新)" \
--notes-file /tmp/release-notes.md \
--latest
echo "已创建 release:$TAG"
fi
echo "url=https://github.com/laincat/AltStore/releases/download/$TAG/apps.json" >> "$GITHUB_OUTPUT"
- name: 同步到 cnb.cool(没配 secret 就跳过)
env:
CNB_TOKEN: ${{ secrets.CNB_TOKEN }}
CNB_USER: ${{ secrets.CNB_USER }}
run: |
set -e
if [ -z "$CNB_TOKEN" ]; then echo "CNB mirror not configured"; exit 0; fi
# 凭据只在这条命令里临时注入:不写进 .git/config,不落盘。
# 前面那个空的 -c credential.helper= 用来清掉系统级 helper。
# 注意:token 里若有单引号会破坏这里的引号,CNB 的 cnb_at_ 令牌是
# [A-Za-z0-9_-],不含引号,安全。
CRED='!f() { echo "username='"${CNB_USER}"'"; echo "password='"${CNB_TOKEN}"'"; }; f'
# 做法是「克隆 cnb 侧 → 覆盖产物 → 提交推送」,而不是直接把 GitHub 的
# 历史整段推过去。两个仓库的历史本来就不相关,硬推会被拒或需要 force;
# 只同步产物则两边历史各自独立,永远不会有分叉冲突。
rm -rf /tmp/cnb-mirror
git -c credential.helper= -c credential.helper="$CRED" \
clone -q "https://cnb.cool/${CNB_REPO}.git" /tmp/cnb-mirror
cp apps.json /tmp/cnb-mirror/apps.json
mkdir -p /tmp/cnb-mirror/cache /tmp/cnb-mirror/tools
cp cache/*.json /tmp/cnb-mirror/cache/
cp tools/*.py /tmp/cnb-mirror/tools/
cp README.md /tmp/cnb-mirror/README.md
cd /tmp/cnb-mirror
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --cached --quiet; then
echo "cnb.cool 侧已是最新,无需同步"
exit 0
fi
git commit -q -m "chore(source): 同步自 GitHub Actions $(date -u '+%Y-%m-%d')"
git -c credential.helper= -c credential.helper="$CRED" push -q origin HEAD:main
echo "已同步到 cnb.cool/${CNB_REPO}"
# 把 apps.json 镜像到 CNB Release:那边 releases 页面此前一直是空的
# (同步只推分支历史,从不建 Release)。CNB 没有 releases/latest 别名,
# 所以固定 tag `latest`,靠覆盖同名附件换稳定直链:
# https://cnb.cool/laincat/AltStore/-/releases/download/latest/apps.json
- name: 镜像 apps.json 到 cnb.cool Release
env:
CNB_TOKEN: ${{ secrets.CNB_TOKEN }}
CNB_REPO_SLUG: ${{ env.CNB_REPO }}
run: |
set -euo pipefail
if [ -z "$CNB_TOKEN" ]; then echo "CNB release mirror not configured"; exit 0; fi
python tools/publish_cnb_release.py --files apps.json --tag latest --ttl 0
- name: 小结
if: always()
run: |
{
echo "### 源文件更新结果"
echo ""
if [ "${{ steps.commit.outputs.changed }}" = "true" ]; then
echo "- ✅ 有更新,已提交"
else
echo "- ⏸ 上游无新版本,未产生提交"
fi
echo "- cnb.cool 同步:见对应步骤日志"
if [ -n "${{ steps.release.outputs.url }}" ]; then
echo "- Release 地址(固定,滚动覆盖):${{ steps.release.outputs.url }}"
fi
} >> "$GITHUB_STEP_SUMMARY"