feat(release): add retry logic for release creation
- Introduces a `create_release` function with retry logic to handle transient failures during release creation. - Improves error handling and logging for better debugging. - Adjusts changelog parsing and workflow steps for clarity and robustness.
This commit is contained in:
@@ -1,13 +1,59 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
create_release() {
|
||||
local -r owner="$1" repo="$2" token="$3" version="$4" body_file="$5"
|
||||
local -i max_attempts=3 delay=5 attempt
|
||||
local http status
|
||||
|
||||
# Release-Body für die JSON-Payload einlesen
|
||||
local body_json
|
||||
body_json=$(tail -n +2 "$body_file" | jq -Rs .)
|
||||
|
||||
for (( attempt=1; attempt<=max_attempts; attempt++ )); do
|
||||
echo "🚀 Try $attempt/$max_attempts: creating release v$version …"
|
||||
|
||||
http=$(curl -sS \
|
||||
-H "Authorization: token $token" \
|
||||
-H "Content-Type: application/json" \
|
||||
-o resp.json -w '%{http_code}' \
|
||||
-X POST "$GITHUB_API_URL/repos/$owner/$repo/releases" \
|
||||
-d @- <<EOF
|
||||
{
|
||||
"tag_name": "v$version",
|
||||
"target_commitish": "main",
|
||||
"name": "Release v$version",
|
||||
"body": $body_json,
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
status=$http
|
||||
if [[ $status == 201 ]]; then
|
||||
echo "✅ Release created (201)."
|
||||
rm -f resp.json
|
||||
return 0
|
||||
elif [[ $status == 409 ]]; then
|
||||
echo "🔁 Release already exists (409) – skipping."
|
||||
rm -f resp.json
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "⚠️ Release attempt failed (HTTP $status)"
|
||||
cat resp.json
|
||||
[[ $attempt -lt $max_attempts ]] || { echo "❌ Giving up."; return 1; }
|
||||
sleep $((delay*attempt)) # Back-off: 5 s, 10 s, 15 s …
|
||||
done
|
||||
}
|
||||
|
||||
VERSION_FILE="VERSION"
|
||||
CHANGELOG_FILE="CHANGELOG.md"
|
||||
CLIFF_CONFIG="cliff.toml"
|
||||
RELEASE_BODY_TMP="$(mktemp)"
|
||||
BODY_OUTPUT_VAR="changelog_body_path"
|
||||
|
||||
# === Schritt 1: Read version from VERSION ===
|
||||
# === Schritt 1: Version lesen ===
|
||||
if [[ ! -f "$VERSION_FILE" ]]; then
|
||||
echo "❌ VERSION file not found"
|
||||
exit 1
|
||||
@@ -15,7 +61,7 @@ fi
|
||||
VERSION="$(<"$VERSION_FILE")"
|
||||
echo "📦 Version: $VERSION"
|
||||
|
||||
# === Schritt 2: Generate changelog for release ===
|
||||
# === Schritt 2: Changelog für Release erzeugen ===
|
||||
echo "📄 Generating changelog for tag v$VERSION"
|
||||
git-cliff -c "$CLIFF_CONFIG" -t "v$VERSION" -o "$CHANGELOG_FILE"
|
||||
|
||||
@@ -26,17 +72,15 @@ awk -v ver="$ESCAPED_VERSION" '
|
||||
print_flag=1
|
||||
line=$0
|
||||
sub(/^## /,"",line)
|
||||
sub(/\\s*\\(.*\\)/, "", line) # entfernt z. B. "(...)" oder "(*)"
|
||||
sub(/\\s*\\(.*\\)/,"",line)
|
||||
print line
|
||||
next
|
||||
}
|
||||
$0 ~ "^## \\[" && $0 !~ "^## \\[" ver "\\]" {
|
||||
print_flag=0
|
||||
}
|
||||
$0 ~ "^## \\[" && $0 !~ "^## \\[" ver "\\]" { print_flag=0 }
|
||||
print_flag
|
||||
' "$CHANGELOG_FILE" > "$RELEASE_BODY_TMP"
|
||||
|
||||
# === Schritt 3: Commit updated changelog ===
|
||||
# === Schritt 3: Changelog committen ===
|
||||
git add "$CHANGELOG_FILE"
|
||||
if git diff --cached --quiet; then
|
||||
echo "✅ No changes to commit"
|
||||
@@ -46,7 +90,7 @@ else
|
||||
git push origin main
|
||||
fi
|
||||
|
||||
# === Schritt 4: Create tag if not exists ===
|
||||
# === Schritt 4: Tag anlegen, falls nötig ===
|
||||
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
|
||||
echo "🔁 Tag v$VERSION already exists, skipping."
|
||||
else
|
||||
@@ -57,9 +101,9 @@ else
|
||||
git push origin "v$VERSION"
|
||||
fi
|
||||
|
||||
# === Schritt 5: Create Gitea release ===
|
||||
OWNER="$(echo "$GITHUB_REPOSITORY" | cut -d/ -f1)"
|
||||
REPO="$(echo "$GITHUB_REPOSITORY" | cut -d/ -f2)"
|
||||
# === Schritt 5: Release anlegen (mit Retry) ===
|
||||
OWNER="${GITHUB_REPOSITORY%/*}"
|
||||
REPO="${GITHUB_REPOSITORY#*/}"
|
||||
TOKEN="${RELEASE_PUBLISH_TOKEN:-$ACTIONS_RUNTIME_TOKEN}"
|
||||
|
||||
if [[ -z "${RELEASE_PUBLISH_TOKEN:-}" ]]; then
|
||||
@@ -69,29 +113,6 @@ if [[ -z "${RELEASE_PUBLISH_TOKEN:-}" ]]; then
|
||||
echo
|
||||
fi
|
||||
|
||||
# Prüfen, ob Release bereits existiert
|
||||
if curl -sf "$GITHUB_API_URL/repos/$OWNER/$REPO/releases/tags/v$VERSION" \
|
||||
-H "Authorization: token $TOKEN" > /dev/null; then
|
||||
echo "🔁 Release for tag v$VERSION already exists, skipping."
|
||||
exit 0
|
||||
fi
|
||||
create_release "$OWNER" "$REPO" "$TOKEN" "$VERSION" "$RELEASE_BODY_TMP"
|
||||
|
||||
echo "🚀 Creating Gitea release for v$VERSION"
|
||||
|
||||
RELEASE_BODY=$(tail -n +2 "$RELEASE_BODY_TMP" | jq -Rs .)
|
||||
|
||||
curl -X POST "$GITHUB_API_URL/repos/$OWNER/$REPO/releases" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d @- <<EOF
|
||||
{
|
||||
"tag_name": "v$VERSION",
|
||||
"target_commitish": "main",
|
||||
"name": "Release v$VERSION",
|
||||
"body": $RELEASE_BODY,
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
|
||||
echo "✅ Release for tag $VERSION created successfully."
|
||||
echo "🎉 Workflow finished successfully."
|
||||
|
Reference in New Issue
Block a user