feat(ci): 发布后自动清理旧版本,server-v* Release 仅保留最近 10 个
Build and Publish Server / build (push) Successful in 1m24s

- 超出部分连同附件与 tag 一并删除,防止磁盘被部署包占满
- 纯 grep/sed 解析,不依赖 python3/jq;已对真实 API 验证提取逻辑
This commit is contained in:
gouki
2026-08-09 20:14:31 +00:00
parent a6bdfee548
commit fb1bd57af0
+29
View File
@@ -151,6 +151,35 @@ jobs:
-o /dev/null -w "附件上传: HTTP %{http_code}\n"
echo "发布地址: https://gitea.neatcn.com/gouki/lunar-mini/releases/tag/$TAG"
- name: Cleanup old releases (keep latest 10)
env:
LUNAR_PUBLISH_TOKEN: ${{ secrets.LUNAR_PUBLISH_TOKEN }}
run: |
# 只保留最近 10 个 server-v* 发布,避免附件无限堆积占满磁盘
API="https://gitea.neatcn.com/api/v1/repos/gouki/lunar-mini"
TOKEN="${LUNAR_PUBLISH_TOKEN:-$GITEA_TOKEN}"
AUTH="Authorization: token $TOKEN"
KEEP=10
# Gitea Release JSON 中 id 与 tag_name 相邻且按新到旧排序;
# 附件随 release 删除一并清理
PAIRS=$(curl -sS -H "$AUTH" "$API/releases?limit=50" \
| grep -oE '"id":[0-9]+,"tag_name":"server-v[^"]*"' || true)
TOTAL=$(printf '%s\n' "$PAIRS" | grep -c '"id"' || true)
if [ "$TOTAL" -le "$KEEP" ]; then
echo "当前 $TOTAL 个发布版本,未超过 $KEEP,无需清理"
exit 0
fi
printf '%s\n' "$PAIRS" | tail -n +$((KEEP + 1)) | while IFS= read -r line; do
[ -z "$line" ] && continue
RID=$(printf '%s' "$line" | grep -oE '"id":[0-9]+' | cut -d: -f2)
RTAG=$(printf '%s' "$line" | sed 's/.*"tag_name":"\([^"]*\)".*/\1/')
curl -sS -X DELETE -H "$AUTH" "$API/releases/$RID" -o /dev/null -w "删除旧发布 $RTAG: HTTP %{http_code}\n"
# 同步删除自动创建的 tag,避免 tag 堆积
curl -sS -X DELETE -H "$AUTH" "$API/tags/$RTAG" -o /dev/null -w "删除 tag $RTAG: HTTP %{http_code}\n"
done
- name: Send success notification
if: success()
env: