diff --git a/rel.sh b/rel.sh index 8dc58c4..24c19d7 100755 --- a/rel.sh +++ b/rel.sh @@ -82,11 +82,35 @@ else exit 1 fi + if [ -z "$GITEA_TOKEN" ]; then echo "⚠️ 未设置 GITEA_TOKEN,只推送了 tag,没有创建 Release" exit 0 fi +# 如果远程已经存在该 Release(通过 tag 查询),先删除它(避免 409 Conflict) +echo "🔍 检查远程 Release 是否已存在..." +check_response=$(curl -s -w "\n%{http_code}" \ + -X GET "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/tags/$VERSION" \ + -H "Authorization: token $GITEA_TOKEN") + +check_http_code=$(echo "$check_response" | tail -n 1) +check_body=$(echo "$check_response" | sed '$d') + +if [ "$check_http_code" -eq 200 ]; then + release_id=$(echo "$check_body" | jq -r '.id') + echo "⚠️ 远程已存在 Release $VERSION (ID: $release_id),正在删除..." + delete_response=$(curl -s -w "\n%{http_code}" \ + -X DELETE "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/$release_id" \ + -H "Authorization: token $GITEA_TOKEN") + delete_http_code=$(echo "$delete_response" | tail -n 1) + if [ "$delete_http_code" -eq 204 ] || [ "$delete_http_code" -eq 200 ]; then + echo "✅ 已删除旧的 Release" + else + echo "⚠️ 删除旧 Release 失败 (HTTP $delete_http_code),继续尝试创建新的 Release" + fi +fi + # 使用 jq 生成正确的 JSON JSON_PAYLOAD=$(echo "$TAG_MESSAGE" | jq -R -s -c --arg version "$VERSION" '{ tag_name: $version, @@ -96,16 +120,61 @@ JSON_PAYLOAD=$(echo "$TAG_MESSAGE" | jq -R -s -c --arg version "$VERSION" '{ prerelease: false }') -response=$(curl -s -o /dev/null -w "%{http_code}" \ +# 尝试创建 Release,若返回 409(冲突),再查询并删除后重试一次 +echo "🔄 正在创建 Release(首次尝试)..." +response_full=$(curl -s -w "\n%{http_code}" \ -X POST "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases" \ -H "Content-Type: application/json" \ -H "Authorization: token $GITEA_TOKEN" \ -d "$JSON_PAYLOAD") -if [ "$response" -eq 201 ]; then +http_code=$(echo "$response_full" | tail -n 1) +resp_body=$(echo "$response_full" | sed '$d') + +if [ "$http_code" -eq 201 ]; then echo "✅ Release 创建成功: $VERSION" else - echo "❌ Release 创建失败,HTTP $response" + if [ "$http_code" -eq 409 ]; then + echo "⚠️ 创建 Release 返回 409 Conflict,尝试删除远程冲突的 Release 并重试..." + # 再次查询 Release id + check_response=$(curl -s -w "\n%{http_code}" \ + -X GET "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/tags/$VERSION" \ + -H "Authorization: token $GITEA_TOKEN") + check_http_code=$(echo "$check_response" | tail -n 1) + check_body=$(echo "$check_response" | sed '$d') + if [ "$check_http_code" -eq 200 ]; then + release_id=$(echo "$check_body" | jq -r '.id') + delete_response=$(curl -s -w "\n%{http_code}" \ + -X DELETE "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/$release_id" \ + -H "Authorization: token $GITEA_TOKEN") + delete_http_code=$(echo "$delete_response" | tail -n 1) + if [ "$delete_http_code" -eq 204 ] || [ "$delete_http_code" -eq 200 ]; then + echo "✅ 已删除冲突的 Release,准备重试创建..." + # 重试创建一次 + retry_response=$(curl -s -w "\n%{http_code}" \ + -X POST "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases" \ + -H "Content-Type: application/json" \ + -H "Authorization: token $GITEA_TOKEN" \ + -d "$JSON_PAYLOAD") + retry_code=$(echo "$retry_response" | tail -n 1) + retry_body=$(echo "$retry_response" | sed '$d') + if [ "$retry_code" -eq 201 ]; then + echo "✅ Release 创建成功 (重试): $VERSION" + else + echo "❌ 重试创建 Release 仍然失败 (HTTP $retry_code)" + echo "响应: $retry_body" + fi + else + echo "❌ 删除冲突 Release 失败 (HTTP $delete_http_code),无法重试" + echo "响应: $delete_response" + fi + else + echo "❌ 查询冲突 Release 失败 (HTTP $check_http_code),响应: $check_body" + fi + else + echo "❌ Release 创建失败,HTTP $http_code" + echo "响应: $resp_body" + fi fi echo ""