Files
cleanup-release-action/cleanup-images.sh
2025-12-01 15:14:56 +08:00

135 lines
4.3 KiB
Bash
Raw Permalink 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"
PACKAGE_NAME="$3"
TOKEN="$4"
KEEP_COUNT="$5"
DRY_RUN="${6:-false}"
# 參數驗證
if [[ -z "$GITEA_SERVER" ]] || [[ -z "$REPOSITORY" ]] || [[ -z "$PACKAGE_NAME" ]] || [[ -z "$TOKEN" ]] || [[ -z "$KEEP_COUNT" ]]; then
echo "❌ 錯誤:缺少必要參數"
echo "使用方式: $0 <gitea-server> <repository> <package-name> <token> <keep-count> [dry-run]"
exit 1
fi
echo "開始清理舊映像,保留最新 $KEEP_COUNT 個版本"
# 提取 owner 名稱
OWNER=$(echo "$REPOSITORY" | cut -d'/' -f1)
echo "Owner: $OWNER"
echo "Package: $PACKAGE_NAME"
# 獲取所有 container 版本並按創建時間排序
echo "正在獲取映像版本列表..."
VERSIONS_JSON=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
"$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME?token=$TOKEN" \
-H "Accept: application/json")
HTTP_STATUS=$(echo "$VERSIONS_JSON" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
VERSIONS_JSON=$(echo "$VERSIONS_JSON" | sed '/HTTP_STATUS:/d')
# 檢查是否成功獲取版本列表
if [[ "$HTTP_STATUS" != "200" ]] || [[ -z "$VERSIONS_JSON" ]] || [[ "$VERSIONS_JSON" == "null" ]]; then
echo "❌ 錯誤:無法獲取 container 版本列表 (HTTP Status: $HTTP_STATUS)"
exit 1
fi
# 獲取並排序所有版本 (按創建時間降序)
SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq -e 'sort_by(.created_at) | reverse')
if [[ $? -ne 0 ]]; then
echo "❌ 錯誤:無法解析版本資料"
exit 1
fi
# 計算總數量
TOTAL_COUNT=$(echo "$SORTED_VERSIONS" | jq 'length')
echo "目前總共有 $TOTAL_COUNT 個映像版本"
# 如果總數量小於等於保留數量,則無需清理
if [[ $TOTAL_COUNT -le $KEEP_COUNT ]]; then
echo "✅ 映像數量 ($TOTAL_COUNT) 未超過保留數量 ($KEEP_COUNT),無需清理"
exit 0
fi
# 計算需要刪除的數量
DELETE_COUNT=$((TOTAL_COUNT - KEEP_COUNT))
echo "需要刪除 $DELETE_COUNT 個舊映像"
# 獲取要刪除的版本 (跳過前 keep-count 個)
TO_DELETE=$(echo "$SORTED_VERSIONS" | jq -c ".[$KEEP_COUNT:][]")
# 初始化刪除計數器
DELETED_COUNT=0
FAILED_COUNT=0
# 處理每個要刪除的版本
while IFS= read -r version; do
if [[ -z "$version" ]] || [[ "$version" == "null" ]]; then
continue
fi
VERSION_ID=$(echo "$version" | jq -r '.id')
VERSION_NAME=$(echo "$version" | jq -r '.version')
CREATED_AT=$(echo "$version" | jq -r '.created_at')
if [[ -z "$VERSION_ID" ]] || [[ "$VERSION_ID" == "null" ]]; then
continue
fi
echo "準備刪除: ID=$VERSION_ID, Version=$VERSION_NAME, Created=$CREATED_AT"
if [[ "$DRY_RUN" == "true" ]]; then
echo "🔍 [模擬執行] 會刪除映像版本: $VERSION_NAME (ID: $VERSION_ID)"
DELETED_COUNT=$((DELETED_COUNT + 1))
else
# 實際刪除
DELETE_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
-X DELETE "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME/$VERSION_NAME?token=$TOKEN" \
-H "Accept: application/json")
DELETE_HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
if [[ "$DELETE_HTTP_STATUS" == "204" ]] || [[ "$DELETE_HTTP_STATUS" == "200" ]]; then
echo "✅ 成功刪除映像版本: $VERSION_NAME (ID: $VERSION_ID)"
DELETED_COUNT=$((DELETED_COUNT + 1))
else
echo "❌ 刪除失敗映像版本: $VERSION_NAME (ID: $VERSION_ID), HTTP狀態: $DELETE_HTTP_STATUS"
FAILED_COUNT=$((FAILED_COUNT + 1))
fi
fi
done <<< "$TO_DELETE"
# 檢查是否有刪除失敗的情況
if [[ $FAILED_COUNT -gt 0 ]]; then
echo ""
echo "❌ 錯誤:有 $FAILED_COUNT 個映像版本刪除失敗"
exit 1
fi
# 輸出最終結果
echo ""
echo "========================================="
if [[ "$DRY_RUN" == "true" ]]; then
echo "🔍 [模擬執行] 總共會刪除 $DELETED_COUNT 個映像版本"
echo "✅ 模擬執行完成"
else
echo "✅ 清理完成,成功刪除了 $DELETED_COUNT 個映像版本"
# 驗證結果
if [[ $DELETED_COUNT -gt 0 ]]; then
echo ""
echo "正在驗證剩餘映像數量..."
VERIFY_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME?token=$TOKEN" \
-H "Accept: application/json")
NEW_TOTAL_COUNT=$(echo "$VERIFY_JSON" | jq 'length')
echo "📊 目前剩餘 $NEW_TOTAL_COUNT 個映像版本(預期: $KEEP_COUNT"
fi
fi
echo "========================================="