Files

98 lines
3.6 KiB
YAML
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.
name: 'Cleanup Releases'
description: '自动清理旧 Release 及附件(兼容 Gitea 和 GitHub,支持保留最新 N 个版本与标签模式匹配)'
author: 'yanhao98'
inputs:
keep_latest:
description: '保留最新的 Release 数量'
required: false
default: '5'
delete_tags:
description: '是否同步删除关联的 Git 标签'
required: false
default: 'true'
delete_tag_pattern:
description: '仅删除匹配特定前缀/模式的标签(如 build-*,留空则匹配全部)'
required: false
default: ''
token:
description: 'API Token(默认使用 github.token)'
required: false
default: ${{ github.token }}
runs:
using: 'composite'
steps:
- name: 自动清理旧 Release
shell: bash
env:
KEEP_LATEST: ${{ inputs.keep_latest }}
DELETE_TAGS: ${{ inputs.delete_tags }}
TAG_PATTERN: ${{ inputs.delete_tag_pattern }}
API_URL: ${{ github.api_url }}
REPOSITORY: ${{ github.repository }}
AUTH_TOKEN: ${{ inputs.token }}
run: |
set -euo pipefail
keep_count="${KEEP_LATEST:-5}"
echo "=== 开始检查 Release 清理(目标保留最新 ${keep_count} 个)==="
# 获取 Release 列表(默认按发布时间降序)
releases_json=$(curl -sS -H "Authorization: token ${AUTH_TOKEN}" \
"${API_URL}/repos/${REPOSITORY}/releases?per_page=100")
if ! echo "$releases_json" | jq -e 'type == "array"' >/dev/null 2>&1; then
echo "::warning::获取 Release 列表失败或返回非数组响应: ${releases_json}"
exit 0
fi
total_count=$(echo "$releases_json" | jq '. | length')
echo "当前仓库共有 Release 数量: ${total_count}"
if [ "$total_count" -le "$keep_count" ]; then
echo "Release 数量未超出保留上限 (${total_count} <= ${keep_count}),无需清理。"
exit 0
fi
# 提取超出保留数量的旧版本
old_items=$(echo "$releases_json" | jq -c ".[${keep_count}:] | .[] | {id: .id, tag: .tag_name, name: .name}")
deleted_count=0
while IFS= read -r item; do
[ -z "$item" ] && continue
rel_id=$(echo "$item" | jq -r '.id')
rel_tag=$(echo "$item" | jq -r '.tag')
rel_name=$(echo "$item" | jq -r '.name')
echo "🗑️ 正在删除旧 Release: [${rel_id}] ${rel_name} (Tag: ${rel_tag}) ..."
# 1. 删除 Release 实体(附件随之被清理,释放底层存储空间)
code=$(curl -sS -o /dev/null -w "%{http_code}" -X DELETE \
-H "Authorization: token ${AUTH_TOKEN}" \
"${API_URL}/repos/${REPOSITORY}/releases/${rel_id}")
echo " └─ 删除 Release 响应: HTTP ${code}"
# 2. 是否同步删除 Tag
if [ "${DELETE_TAGS}" = "true" ]; then
should_delete_tag=true
if [ -n "${TAG_PATTERN}" ]; then
if [[ ! "${rel_tag}" == ${TAG_PATTERN} ]]; then
should_delete_tag=false
echo " └─ 标签 '${rel_tag}' 不匹配模式 '${TAG_PATTERN}',予以保留"
fi
fi
if [ "${should_delete_tag}" = true ]; then
tag_code=$(curl -sS -o /dev/null -w "%{http_code}" -X DELETE \
-H "Authorization: token ${AUTH_TOKEN}" \
"${API_URL}/repos/${REPOSITORY}/tags/${rel_tag}" || true)
echo " └─ 删除 Tag 响应: HTTP ${tag_code}"
fi
fi
deleted_count=$((deleted_count + 1))
done <<< "$old_items"
echo "✅ 清理完成,共清理 ${deleted_count} 个历史版本产物"