feat: 優化程序

This commit is contained in:
Jeffery
2025-12-01 14:32:01 +08:00
parent 806e385b86
commit 789d08a0a6
2 changed files with 123 additions and 70 deletions

View File

@@ -1,13 +1,20 @@
#!/bin/bash
set -e
set -euo pipefail
GITEA_SERVER="$1"
REPOSITORY="$2"
PACKAGE_NAME="$3"
TOKEN="$4"
KEEP_COUNT="$5"
DRY_RUN="$6"
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 個版本"
@@ -18,26 +25,35 @@ echo "Owner: $OWNER"
echo "Package: $PACKAGE_NAME"
# 獲取所有 container 版本並按創建時間排序
VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME?token=$TOKEN" \
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 [ $? -ne 0 ] || [ -z "$VERSIONS_JSON" ]; then
echo "錯誤:無法獲取 container 版本列表"
if [[ "$HTTP_STATUS" != "200" ]] || [[ -z "$VERSIONS_JSON" ]] || [[ "$VERSIONS_JSON" == "null" ]]; then
echo "錯誤:無法獲取 container 版本列表 (HTTP Status: $HTTP_STATUS)"
exit 1
fi
# 獲取並排序所有版本 (按創建時間降序)
# API 直接返回版本陣列,不需要 .versions
SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq 'sort_by(.created_at) | reverse')
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),無需清理"
if [[ $TOTAL_COUNT -le $KEEP_COUNT ]]; then
echo "映像數量 ($TOTAL_COUNT) 未超過保留數量 ($KEEP_COUNT),無需清理"
exit 0
fi
@@ -46,15 +62,15 @@ DELETE_COUNT=$((TOTAL_COUNT - KEEP_COUNT))
echo "需要刪除 $DELETE_COUNT 個舊映像"
# 獲取要刪除的版本 (跳過前 keep-count 個)
TO_DELETE=$(echo "$SORTED_VERSIONS" | jq -r ".[$KEEP_COUNT:]")
TO_DELETE=$(echo "$SORTED_VERSIONS" | jq -c ".[$KEEP_COUNT:][]")
# 初始化刪除計數器和列表
DELETED_COUNT=0
DELETED_LIST="[]"
declare -a DELETED_VERSIONS
# 處理每個要刪除的版本
echo "$TO_DELETE" | jq -c '.[]' | while read -r version; do
if [ -z "$version" ] || [ "$version" = "null" ]; then
while IFS= read -r version; do
if [[ -z "$version" ]] || [[ "$version" == "null" ]]; then
continue
fi
@@ -62,45 +78,58 @@ echo "$TO_DELETE" | jq -c '.[]' | while read -r version; do
VERSION_NAME=$(echo "$version" | jq -r '.version')
CREATED_AT=$(echo "$version" | jq -r '.created_at')
if [ -z "$VERSION_ID" ] || [ "$VERSION_ID" = "null" ]; then
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
if [[ "$DRY_RUN" == "true" ]]; then
echo "🔍 [模擬執行] 會刪除映像版本: $VERSION_NAME (ID: $VERSION_ID)"
DELETED_COUNT=$((DELETED_COUNT + 1))
DELETED_VERSIONS+=("$VERSION_NAME")
else
# 實際刪除
DELETE_RESPONSE=$(curl -s -w "HTTP_STATUS:%{http_code}" \
DELETE_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
-X DELETE "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME/$VERSION_ID?token=$TOKEN" \
-H "Accept: application/json")
HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
DELETE_HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
if [ "$HTTP_STATUS" = "204" ] || [ "$HTTP_STATUS" = "200" ]; then
if [[ "$DELETE_HTTP_STATUS" == "204" ]] || [[ "$DELETE_HTTP_STATUS" == "200" ]]; then
echo "✅ 成功刪除映像版本: $VERSION_NAME (ID: $VERSION_ID)"
DELETED_COUNT=$((DELETED_COUNT + 1))
# 更新已刪除列表
DELETED_LIST=$(echo "$DELETED_LIST" | jq ". + [{\"id\": \"$VERSION_ID\", \"version\": \"$VERSION_NAME\", \"created_at\": \"$CREATED_AT\"}]")
DELETED_VERSIONS+=("$VERSION_NAME")
else
echo "❌ 刪除失敗映像版本: $VERSION_NAME (ID: $VERSION_ID), HTTP狀態: $HTTP_STATUS"
echo "❌ 刪除失敗映像版本: $VERSION_NAME (ID: $VERSION_ID), HTTP狀態: $DELETE_HTTP_STATUS"
fi
fi
done
done <<< "$TO_DELETE"
# 由於 while 迴圈在子 shell 中執行,需要重新計算
if [ "$DRY_RUN" = "true" ]; then
FINAL_DELETE_COUNT=$(echo "$TO_DELETE" | jq 'length')
echo "🔍 [模擬執行] 總共會刪除 $FINAL_DELETE_COUNT 個映像版本"
# 輸出最終結果
echo ""
echo "========================================="
if [[ "$DRY_RUN" == "true" ]]; then
echo "🔍 [模擬執行] 總共會刪除 $DELETED_COUNT 個映像版本"
if [[ ${#DELETED_VERSIONS[@]} -gt 0 ]]; then
echo "待刪除版本列表:"
printf ' - %s\n' "${DELETED_VERSIONS[@]}"
fi
else
# 重新獲取並計算實際刪除的數量
NEW_VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME?token=$TOKEN" \
-H "Accept: application/json")
NEW_TOTAL_COUNT=$(echo "$NEW_VERSIONS_JSON" | jq 'length')
ACTUAL_DELETED=$((TOTAL_COUNT - NEW_TOTAL_COUNT))
echo "✅ 清理完成,成功刪除了 $DELETED_COUNT 個映像版本"
if [[ ${#DELETED_VERSIONS[@]} -gt 0 ]]; then
echo "已刪除版本列表:"
printf ' - %s\n' "${DELETED_VERSIONS[@]}"
fi
echo "✅ 清理完成,實際刪除了 $ACTUAL_DELETED 個映像版本"
# 驗證結果
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 "========================================="