Compare commits
2 Commits
e40a7adad6
...
v0.2.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5333d4562 | ||
|
|
26b91397f1 |
19
action.yml
19
action.yml
@@ -28,10 +28,25 @@ runs:
|
||||
- name: 清理舊版本
|
||||
id: cleanup
|
||||
shell: bash
|
||||
run: chmod +x "${{ github.action_path }}/cleanup-releases.sh" && "${{ github.action_path }}/cleanup-releases.sh" "${{ inputs.gitea-server }}" "${{ inputs.repository }}" "${{ inputs.token }}" "${{ inputs.keep-count }}" "${{ inputs.dry-run }}"
|
||||
run: |
|
||||
chmod +x "${{ github.action_path }}/cleanup-releases.sh"
|
||||
"${{ github.action_path }}/cleanup-releases.sh" \
|
||||
"${{ inputs.gitea-server }}" \
|
||||
"${{ inputs.repository }}" \
|
||||
"${{ inputs.token }}" \
|
||||
"${{ inputs.keep-count }}" \
|
||||
"${{ inputs.dry-run }}"
|
||||
|
||||
- name: 清理舊映像
|
||||
id: cleanup-images
|
||||
if: inputs.package-name != ''
|
||||
shell: bash
|
||||
run: 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 }}"
|
||||
run: |
|
||||
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 }}"
|
||||
|
||||
@@ -18,8 +18,7 @@ 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" \
|
||||
VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME?token=$TOKEN" \
|
||||
-H "Accept: application/json")
|
||||
|
||||
# 檢查是否成功獲取版本列表
|
||||
@@ -29,7 +28,8 @@ if [ $? -ne 0 ] || [ -z "$VERSIONS_JSON" ]; then
|
||||
fi
|
||||
|
||||
# 獲取並排序所有版本 (按創建時間降序)
|
||||
SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq '.versions | sort_by(.created_at) | reverse')
|
||||
# API 直接返回版本陣列,不需要 .versions
|
||||
SORTED_VERSIONS=$(echo "$VERSIONS_JSON" | jq 'sort_by(.created_at) | reverse')
|
||||
|
||||
# 計算總數量
|
||||
TOTAL_COUNT=$(echo "$SORTED_VERSIONS" | jq 'length')
|
||||
@@ -46,20 +46,20 @@ 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}")
|
||||
TO_DELETE=$(echo "$SORTED_VERSIONS" | jq -r ".[$KEEP_COUNT:]")
|
||||
|
||||
# 初始化刪除計數器和列表
|
||||
DELETED_COUNT=0
|
||||
DELETED_LIST="[]"
|
||||
|
||||
# 處理每個要刪除的版本
|
||||
echo "$TO_DELETE" | jq -c '.' | while read -r version; do
|
||||
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')
|
||||
VERSION_NAME=$(echo "$version" | jq -r '.version')
|
||||
CREATED_AT=$(echo "$version" | jq -r '.created_at')
|
||||
|
||||
if [ -z "$VERSION_ID" ] || [ "$VERSION_ID" = "null" ]; then
|
||||
@@ -74,8 +74,8 @@ echo "$TO_DELETE" | jq -c '.' | while read -r version; do
|
||||
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")
|
||||
-X DELETE "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME/$VERSION_NAME?token=$TOKEN" \
|
||||
-H "Accept: application/json")
|
||||
|
||||
HTTP_STATUS=$(echo "$DELETE_RESPONSE" | grep -o "HTTP_STATUS:[0-9]*" | cut -d: -f2)
|
||||
|
||||
@@ -93,14 +93,13 @@ done
|
||||
|
||||
# 由於 while 迴圈在子 shell 中執行,需要重新計算
|
||||
if [ "$DRY_RUN" = "true" ]; then
|
||||
FINAL_DELETE_COUNT=$(echo "$TO_DELETE" | jq -s 'length')
|
||||
FINAL_DELETE_COUNT=$(echo "$TO_DELETE" | jq 'length')
|
||||
echo "🔍 [模擬執行] 總共會刪除 $FINAL_DELETE_COUNT 個映像版本"
|
||||
else
|
||||
# 重新獲取並計算實際刪除的數量
|
||||
NEW_VERSIONS_JSON=$(curl -s "$GITEA_SERVER/api/v1/packages/$OWNER/container/$PACKAGE_NAME" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
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 '.versions | length')
|
||||
NEW_TOTAL_COUNT=$(echo "$NEW_VERSIONS_JSON" | jq 'length')
|
||||
ACTUAL_DELETED=$((TOTAL_COUNT - NEW_TOTAL_COUNT))
|
||||
|
||||
echo "✅ 清理完成,實際刪除了 $ACTUAL_DELETED 個映像版本"
|
||||
|
||||
Reference in New Issue
Block a user