Files
cleanup-release/cleanup-releases.sh
2025-12-01 14:32:01 +08:00

122 lines
4.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -euo pipefail
GITEA_SERVER="$1"
REPOSITORY="$2"
TOKEN="$3"
KEEP_COUNT="$4"
DRY_RUN="${5:-false}"
# 參數驗證
if [[ -z "$GITEA_SERVER" ]] || [[ -z "$REPOSITORY" ]] || [[ -z "$TOKEN" ]] || [[ -z "$KEEP_COUNT" ]]; then
echo "❌ 錯誤:缺少必要參數"
echo "使用方式: $0 <gitea-server> <repository> <token> <keep-count> [dry-run]"
exit 1
fi
echo "開始清理舊版本,保留最新 $KEEP_COUNT 個版本"
# 獲取所有 releases 並按創建時間排序
echo "正在獲取 releases 列表..."
RELEASES_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
"$GITEA_SERVER/api/v1/repos/$REPOSITORY/releases" \
-H "Authorization: token $TOKEN" \
-H "Accept: application/json")
HTTP_STATUS=$(echo "$RELEASES_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
RELEASES_JSON=$(echo "$RELEASES_RESPONSE" | sed '/HTTP_STATUS:/d')
# 檢查是否成功獲取 releases
if [[ "$HTTP_STATUS" != "200" ]] || [[ -z "$RELEASES_JSON" ]] || [[ "$RELEASES_JSON" == "null" ]]; then
echo "❌ 錯誤:無法獲取 releases 列表 (HTTP Status: $HTTP_STATUS)"
exit 1
fi
# 計算總數量並排序(按創建時間降序,最新的在前)
SORTED_RELEASES=$(echo "$RELEASES_JSON" | jq -e 'sort_by(.created_at) | reverse')
if [[ $? -ne 0 ]]; then
echo "❌ 錯誤:無法解析 releases 資料"
exit 1
fi
TOTAL_COUNT=$(echo "$SORTED_RELEASES" | jq 'length')
echo "目前總共有 $TOTAL_COUNT 個 releases"
# 如果總數量小於等於保留數量,則無需清理
if [[ $TOTAL_COUNT -le $KEEP_COUNT ]]; then
echo "✅ releases 數量 ($TOTAL_COUNT) 未超過保留數量 ($KEEP_COUNT),無需清理"
exit 0
fi
# 計算需要刪除的數量
DELETE_COUNT=$((TOTAL_COUNT - KEEP_COUNT))
echo "需要刪除 $DELETE_COUNT 個舊版本"
# 獲取要刪除的 releases跳過前 keep-count 個)
TO_DELETE=$(echo "$SORTED_RELEASES" | jq -c ".[$KEEP_COUNT:]")
# 初始化刪除計數器
DELETED_COUNT=0
FAILED_COUNT=0
# 處理每個要刪除的 release
echo "$TO_DELETE" | jq -c '.[]' | while IFS= read -r release; do
if [[ -z "$release" ]] || [[ "$release" == "null" ]]; then
continue
fi
RELEASE_ID=$(echo "$release" | jq -r '.id')
RELEASE_TAG=$(echo "$release" | jq -r '.tag_name')
RELEASE_NAME=$(echo "$release" | jq -r '.name // "未命名"')
if [[ -z "$RELEASE_ID" ]] || [[ "$RELEASE_ID" == "null" ]]; then
continue
fi
if [[ "$DRY_RUN" == "true" ]]; then
echo "🔍 [模擬執行] 會刪除 release: $RELEASE_TAG ($RELEASE_NAME)"
else
# 實際刪除
DELETE_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
-X DELETE "$GITEA_SERVER/api/v1/repos/$REPOSITORY/releases/$RELEASE_ID" \
-H "Authorization: token $TOKEN")
HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
if [[ "$HTTP_STATUS" == "204" ]] || [[ "$HTTP_STATUS" == "200" ]]; then
echo "✅ 成功刪除 release: $RELEASE_TAG ($RELEASE_NAME)"
else
echo "❌ 刪除失敗 release: $RELEASE_TAG ($RELEASE_NAME), HTTP 狀態: $HTTP_STATUS"
fi
fi
done
# 輸出最終結果
if [[ "$DRY_RUN" == "true" ]]; then
echo ""
echo "🔍 [模擬執行] 總共會刪除 $DELETE_COUNT 個版本"
echo "✅ 模擬執行完成"
else
# 重新獲取並計算實際刪除的數量
echo ""
echo "正在驗證刪除結果..."
NEW_RELEASES_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
"$GITEA_SERVER/api/v1/repos/$REPOSITORY/releases" \
-H "Authorization: token $TOKEN" \
-H "Accept: application/json")
NEW_HTTP_STATUS=$(echo "$NEW_RELEASES_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
NEW_RELEASES_JSON=$(echo "$NEW_RELEASES_RESPONSE" | sed '/HTTP_STATUS:/d')
if [[ "$NEW_HTTP_STATUS" == "200" ]] && [[ -n "$NEW_RELEASES_JSON" ]]; then
NEW_TOTAL_COUNT=$(echo "$NEW_RELEASES_JSON" | jq 'length')
ACTUAL_DELETED=$((TOTAL_COUNT - NEW_TOTAL_COUNT))
echo "✅ 清理完成!實際刪除了 $ACTUAL_DELETED 個版本"
echo "📊 目前剩餘 $NEW_TOTAL_COUNT 個 releases"
else
echo "✅ 清理完成(無法驗證最終數量)"
fi
fi