update at 2025-10-15 16:24:30

This commit is contained in:
douboer
2025-10-15 16:24:30 +08:00
parent a4204af0b9
commit c38c189212

75
rel.sh
View File

@@ -82,11 +82,35 @@ else
exit 1 exit 1
fi fi
if [ -z "$GITEA_TOKEN" ]; then if [ -z "$GITEA_TOKEN" ]; then
echo "⚠️ 未设置 GITEA_TOKEN只推送了 tag没有创建 Release" echo "⚠️ 未设置 GITEA_TOKEN只推送了 tag没有创建 Release"
exit 0 exit 0
fi 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 # 使用 jq 生成正确的 JSON
JSON_PAYLOAD=$(echo "$TAG_MESSAGE" | jq -R -s -c --arg version "$VERSION" '{ JSON_PAYLOAD=$(echo "$TAG_MESSAGE" | jq -R -s -c --arg version "$VERSION" '{
tag_name: $version, tag_name: $version,
@@ -96,16 +120,61 @@ JSON_PAYLOAD=$(echo "$TAG_MESSAGE" | jq -R -s -c --arg version "$VERSION" '{
prerelease: false 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" \ -X POST "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: token $GITEA_TOKEN" \ -H "Authorization: token $GITEA_TOKEN" \
-d "$JSON_PAYLOAD") -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" echo "✅ Release 创建成功: $VERSION"
else 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 fi
echo "" echo ""