From 1180cacd8173790efd8e458d470614873a840f89 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Mon, 1 Dec 2025 11:42:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=B9=E7=82=BA=20script=20=E5=9F=B7?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- action.yml | 106 ++++----------------------------------------- cleanup-images.sh | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 98 deletions(-) create mode 100644 cleanup-images.sh diff --git a/action.yml b/action.yml index b773a8b..4cb4ac0 100644 --- a/action.yml +++ b/action.yml @@ -122,101 +122,11 @@ runs: if: ${{ inputs.package-name != '' }} shell: bash run: | - - echo "開始清理舊映像,保留最新 ${{ inputs.keep-count }} 個版本" - - # 提取 owner 名稱 - OWNER=$(echo "${{ inputs.repository }}" | cut -d'/' -f1) - PACKAGE_NAME="${{ inputs.package-name }}" - - echo "Owner: $OWNER" - echo "Package: $PACKAGE_NAME" - - # 獲取所有 container 版本並按創建時間排序 - VERSIONS_JSON=$(curl -s "${{ inputs.gitea-server }}/api/v1/packages/$OWNER/container/$PACKAGE_NAME" \ - -H "Authorization: token ${{ inputs.token }}" \ - -H "Accept: application/json") - - # 檢查是否成功獲取版本列表 - if [ $? -ne 0 ] || [ -z "$VERSIONS_JSON" ]; then - echo "錯誤:無法獲取 container 版本列表" - exit 1 - fi - - # 獲取並排序所有版本 (按創建時間降序) - SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq '.versions | sort_by(.created_at) | reverse') - - # 計算總數量 - TOTAL_COUNT=$(echo "$SORTED_VERSIONS" | jq 'length') - echo "目前總共有 $TOTAL_COUNT 個映像版本" - - # 如果總數量小於等於保留數量,則無需清理 - if [ $TOTAL_COUNT -le ${{ inputs.keep-count }} ]; then - echo "映像數量 ($TOTAL_COUNT) 未超過保留數量 (${{ inputs.keep-count }}),無需清理" - exit 0 - fi - - # 計算需要刪除的數量 - DELETE_COUNT=$((TOTAL_COUNT - ${{ inputs.keep-count }})) - echo "需要刪除 $DELETE_COUNT 個舊映像" - - # 獲取要刪除的版本 (跳過前 keep-count 個) - TO_DELETE=$(echo "$SORTED_VERSIONS" | jq -r ".[${{ inputs.keep-count }}:] | .[] | {id: .id, name: .name, created_at: .created_at}") - - # 初始化刪除計數器和列表 - DELETED_COUNT=0 - DELETED_LIST="[]" - - # 處理每個要刪除的版本 - echo "$TO_DELETE" | jq -c '.' | while 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 '.name') - 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 [ "${{ inputs.dry-run }}" = "true" ]; then - echo "🔍 [模擬執行] 會刪除映像版本: $VERSION_NAME (ID: $VERSION_ID)" - DELETED_COUNT=$((DELETED_COUNT + 1)) - else - # 實際刪除 - DELETE_RESPONSE=$(curl -s -w "HTTP_STATUS:%{http_code}" \ - -X DELETE "${{ inputs.gitea-server }}/api/v1/packages/$OWNER/container/$PACKAGE_NAME/$VERSION_ID" \ - -H "Authorization: token ${{ inputs.token }}") - - HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2) - - if [ "$HTTP_STATUS" = "204" ] || [ "$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\"}]") - else - echo "❌ 刪除失敗映像版本: $VERSION_NAME (ID: $VERSION_ID), HTTP狀態: $HTTP_STATUS" - fi - fi - done - - # 由於 while 迴圈在子 shell 中執行,需要重新計算 - if [ "${{ inputs.dry-run }}" = "true" ]; then - FINAL_DELETE_COUNT=$(echo "$TO_DELETE" | jq 'length') - echo "🔍 [模擬執行] 總共會刪除 $FINAL_DELETE_COUNT 個映像版本" - else - # 重新獲取並計算實際刪除的數量 - NEW_VERSIONS_JSON=$(curl -s "${{ inputs.gitea-server }}/api/v1/packages/$OWNER/container/$PACKAGE_NAME" \ - -H "Authorization: token ${{ inputs.token }}" \ - -H "Accept: application/json") - NEW_TOTAL_COUNT=$(echo "$NEW_VERSIONS_JSON" | jq '.versions | length') - ACTUAL_DELETED=$((TOTAL_COUNT - NEW_TOTAL_COUNT)) - - echo "✅ 清理完成,實際刪除了 $ACTUAL_DELETED 個映像版本" - fi + chmod +x "${{ github.action_path }}/cleanup-images.sh" + "${{ github.action_path }}/cleanup-images.sh" \ + "${{ inputs.gitea-server }}" \ + "${{ inputs.repository }}" \ + "${{ inputs.package-name }}" \ + "${{ inputs.token }}" \ + "${{ inputs.keep-count }}" \ + "${{ inputs.dry-run }}" diff --git a/cleanup-images.sh b/cleanup-images.sh new file mode 100644 index 0000000..4e3d7cf --- /dev/null +++ b/cleanup-images.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +set -e + +GITEA_SERVER="$1" +REPOSITORY="$2" +PACKAGE_NAME="$3" +TOKEN="$4" +KEEP_COUNT="$5" +DRY_RUN="$6" + +echo "開始清理舊映像,保留最新 $KEEP_COUNT 個版本" + +# 提取 owner 名稱 +OWNER=$(echo "$REPOSITORY" | cut -d'/' -f1) + +echo "Owner: $OWNER" +echo "Package: $PACKAGE_NAME" + +# 獲取所有 container 版本並按創建時間排序 +VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME" \ + -H "Authorization: token $TOKEN" \ + -H "Accept: application/json") + +# 檢查是否成功獲取版本列表 +if [ $? -ne 0 ] || [ -z "$VERSIONS_JSON" ]; then + echo "錯誤:無法獲取 container 版本列表" + exit 1 +fi + +# 獲取並排序所有版本 (按創建時間降序) +SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq '.versions | sort_by(.created_at) | reverse') + +# 計算總數量 +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 -r ".[$KEEP_COUNT:] | .[] | {id: .id, name: .name, created_at: .created_at}") + +# 初始化刪除計數器和列表 +DELETED_COUNT=0 +DELETED_LIST="[]" + +# 處理每個要刪除的版本 +echo "$TO_DELETE" | jq -c '.' | while 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 '.name') + 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 "HTTP_STATUS:%{http_code}" \ + -X DELETE "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME/$VERSION_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 "✅ 成功刪除映像版本: $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\"}]") + else + echo "❌ 刪除失敗映像版本: $VERSION_NAME (ID: $VERSION_ID), HTTP狀態: $HTTP_STATUS" + fi + fi +done + +# 由於 while 迴圈在子 shell 中執行,需要重新計算 +if [ "$DRY_RUN" = "true" ]; then + FINAL_DELETE_COUNT=$(echo "$TO_DELETE" | jq -s 'length') + echo "🔍 [模擬執行] 總共會刪除 $FINAL_DELETE_COUNT 個映像版本" +else + # 重新獲取並計算實際刪除的數量 + NEW_VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME" \ + -H "Authorization: token $TOKEN" \ + -H "Accept: application/json") + NEW_TOTAL_COUNT=$(echo "$NEW_VERSIONS_JSON" | jq '.versions | length') + ACTUAL_DELETED=$((TOTAL_COUNT - NEW_TOTAL_COUNT)) + + echo "✅ 清理完成,實際刪除了 $ACTUAL_DELETED 個映像版本" +fi