diff --git a/.gitea/workflows/server-deploy.yml b/.gitea/workflows/server-deploy.yml index 0d11d97..5a914a3 100644 --- a/.gitea/workflows/server-deploy.yml +++ b/.gitea/workflows/server-deploy.yml @@ -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: