From 2d542733f92f5ce620da1d2eadefc6c870f9e2d8 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 1 Jun 2026 10:06:08 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=B8=85=E7=90=86=E9=A2=84=E5=8F=91=E5=B8=83=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=9A=84=E8=84=9A=E6=9C=AC=EF=BC=8C=E6=94=AF=E6=8C=81=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=9C=AA=E7=BB=91=E5=AE=9A=E7=9A=84=E6=AD=A3=E5=BC=8F?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=92=8C=E6=82=AC=E7=A9=BA=E7=9A=84=20GitHub?= =?UTF-8?q?=20=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cleanup-prerelease-tags.yml | 72 ++++++++++++++----- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/.github/workflows/cleanup-prerelease-tags.yml b/.github/workflows/cleanup-prerelease-tags.yml index e3a04e89..c604132a 100644 --- a/.github/workflows/cleanup-prerelease-tags.yml +++ b/.github/workflows/cleanup-prerelease-tags.yml @@ -29,32 +29,66 @@ jobs: echo "is_prerelease=true" >> "$GITHUB_OUTPUT" fi - - name: Delete prerelease releases and tags + - name: Delete prerelease, dangling, and unbound releases/tags if: steps.version.outputs.should_run == 'true' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CONFIRM: ${{ github.event.inputs.confirm }} run: | - - mapfile -t TAGS < <(git tag --list 'v*' | sort -V) - DELETED=0 + # Fetch all tags from remote to ensure full synchronization + git fetch --tags --force - 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 - echo "Keep formal release tag: $TAG" - continue - fi - - if gh release view "$TAG" >/dev/null 2>&1; then - echo "Delete prerelease release: $TAG" - gh release delete "$TAG" --yes + # Formal release tag + if ! contains_element "$TAG" "${GH_RELEASES[@]}"; then + echo "Delete formal tag not bound to any GitHub release: $TAG" + git push origin --delete "refs/tags/$TAG" || true + git tag -d "$TAG" || true + DELETED_TAGS=$((DELETED_TAGS + 1)) + else + echo "Keep formal release tag (bound to release): $TAG" + fi else - echo "No GitHub Release found for $TAG" - fi + # Prerelease tag + 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" - git push origin --delete "refs/tags/$TAG" - DELETED=$((DELETED + 1)) + echo "Delete prerelease tag: $TAG" + git push origin --delete "refs/tags/$TAG" || true + git tag -d "$TAG" || true + DELETED_TAGS=$((DELETED_TAGS + 1)) + fi 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)."