[优化] 更新清理预发布标签的脚本,支持删除未绑定的正式标签和悬空的 GitHub 发布

This commit is contained in:
ryan
2026-06-01 10:06:08 +08:00
parent c677edba06
commit 2d542733f9
+53 -19
View File
@@ -29,32 +29,66 @@ jobs:
echo "is_prerelease=true" >> "$GITHUB_OUTPUT" echo "is_prerelease=true" >> "$GITHUB_OUTPUT"
fi fi
- name: Delete prerelease releases and tags - name: Delete prerelease, dangling, and unbound releases/tags
if: steps.version.outputs.should_run == 'true' if: steps.version.outputs.should_run == 'true'
env: env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CONFIRM: ${{ github.event.inputs.confirm }}
run: | run: |
# Fetch all tags from remote to ensure full synchronization
mapfile -t TAGS < <(git tag --list 'v*' | sort -V) git fetch --tags --force
DELETED=0
for TAG in "${TAGS[@]}"; do # Get all local/remote git tags starting with 'v'
mapfile -t GIT_TAGS < <(git tag --list 'v*' | sort -V)
# Get all GitHub releases (tags associated with releases)
mapfile -t GH_RELEASES < <(gh release list --limit 1000 --json tagName --jq '.[].tagName' 2>/dev/null || true)
# Helper function to check array containment
contains_element() {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
DELETED_TAGS=0
DELETED_RELEASES=0
echo "=== Phase 1: Checking and cleaning Git tags ==="
for TAG in "${GIT_TAGS[@]}"; do
if [[ "$TAG" =~ ^v[0-9]+(\.[0-9]+)*$ ]]; then if [[ "$TAG" =~ ^v[0-9]+(\.[0-9]+)*$ ]]; then
echo "Keep formal release tag: $TAG" # Formal release tag
continue if ! contains_element "$TAG" "${GH_RELEASES[@]}"; then
fi echo "Delete formal tag not bound to any GitHub release: $TAG"
git push origin --delete "refs/tags/$TAG" || true
if gh release view "$TAG" >/dev/null 2>&1; then git tag -d "$TAG" || true
echo "Delete prerelease release: $TAG" DELETED_TAGS=$((DELETED_TAGS + 1))
gh release delete "$TAG" --yes else
echo "Keep formal release tag (bound to release): $TAG"
fi
else else
echo "No GitHub Release found for $TAG" # Prerelease tag
fi if contains_element "$TAG" "${GH_RELEASES[@]}"; then
echo "Delete prerelease release: $TAG"
gh release delete "$TAG" --yes || true
DELETED_RELEASES=$((DELETED_RELEASES + 1))
fi
echo "Delete prerelease tag: $TAG" echo "Delete prerelease tag: $TAG"
git push origin --delete "refs/tags/$TAG" git push origin --delete "refs/tags/$TAG" || true
DELETED=$((DELETED + 1)) git tag -d "$TAG" || true
DELETED_TAGS=$((DELETED_TAGS + 1))
fi
done done
echo "Deleted $DELETED prerelease tag(s)." echo "=== Phase 2: Checking and cleaning dangling GitHub releases ==="
for REL_TAG in "${GH_RELEASES[@]}"; do
if ! contains_element "$REL_TAG" "${GIT_TAGS[@]}"; then
echo "Delete GitHub release not bound to any Git tag: $REL_TAG"
gh release delete "$REL_TAG" --yes || true
DELETED_RELEASES=$((DELETED_RELEASES + 1))
fi
done
echo "=== Summary ==="
echo "Successfully deleted $DELETED_TAGS tag(s) and $DELETED_RELEASES release(s)."