fix(release): make Release.yml robust to curl exit codes #23
+122
-123
@@ -87,168 +87,167 @@ jobs:
|
|||||||
OWNER=$(echo "${REPOSITORY}" | cut -d'/' -f1)
|
OWNER=$(echo "${REPOSITORY}" | cut -d'/' -f1)
|
||||||
REPO=$(echo "${REPOSITORY}" | cut -d'/' -f2)
|
REPO=$(echo "${REPOSITORY}" | cut -d'/' -f2)
|
||||||
API_URL="${GITEA_URL}/api/v1/repos/${OWNER}/${REPO}"
|
API_URL="${GITEA_URL}/api/v1/repos/${OWNER}/${REPO}"
|
||||||
|
|
||||||
# Fallback to main if DEFAULT_BRANCH is empty
|
|
||||||
BASE_BRANCH="${DEFAULT_BRANCH:-main}"
|
BASE_BRANCH="${DEFAULT_BRANCH:-main}"
|
||||||
echo "Using base branch: ${BASE_BRANCH}"
|
echo "Using base branch: ${BASE_BRANCH}"
|
||||||
|
|
||||||
TITLE="chore(release): prepare for ${VERSION}"
|
TITLE="chore(release): prepare for ${VERSION}"
|
||||||
# Read CHANGES.md content and add note (jq --arg will handle JSON escaping)
|
|
||||||
CHANGES_CONTENT=$(cat CHANGES.md)
|
CHANGES_CONTENT=$(cat CHANGES.md)
|
||||||
PR_NOTE="**Note:** Please use **Squash Merge** when merging this PR."
|
PR_NOTE="**Note:** Please use **Squash Merge** when merging this PR."
|
||||||
DESCRIPTION="${CHANGES_CONTENT}"$'\n\n---\n\n'"${PR_NOTE}"
|
DESCRIPTION="${CHANGES_CONTENT}"$'\n\n---\n\n'"${PR_NOTE}"
|
||||||
|
|
||||||
# Delete existing next-release branch to start fresh (auto-closes any open PR)
|
|
||||||
echo "Checking for existing next-release branch..."
|
|
||||||
BRANCH_CHECK=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused -w "%{http_code}" -o /dev/null \
|
|
||||||
-H "Authorization: token ${TOKEN}" \
|
|
||||||
"${API_URL}/branches/next-release")
|
|
||||||
if [ "${BRANCH_CHECK}" = "200" ]; then
|
|
||||||
echo "Deleting existing next-release branch..."
|
|
||||||
curl -sf --retry 3 --retry-delay 2 --retry-connrefused -X DELETE \
|
|
||||||
-H "Authorization: token ${TOKEN}" \
|
|
||||||
"${API_URL}/branches/next-release"
|
|
||||||
echo "Branch deleted"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Prepare CHANGELOG.md content
|
|
||||||
CHANGELOG_CONTENT=$(base64 -w0 < CHANGELOG.md)
|
CHANGELOG_CONTENT=$(base64 -w0 < CHANGELOG.md)
|
||||||
|
VERSION_CONTENT=$(jq -n --arg v "${VERSION}" '{"version":$v}' | base64 -w0)
|
||||||
|
|
||||||
# Prepare .version content
|
# api_call METHOD PATH [JSON_BODY]
|
||||||
VERSION_JSON=$(jq -n --arg v "${VERSION}" '{"version":$v}')
|
# Stdout: first line "<http_code>|<curl_rc>", then response body.
|
||||||
VERSION_CONTENT=$(echo "${VERSION_JSON}" | base64 -w0)
|
# Never returns non-zero so callers must inspect http_code; this
|
||||||
|
# prevents curl exit codes (e.g. CURLE_WRITE_ERROR / 23) from
|
||||||
|
# killing the script via `set -e` inside command substitutions.
|
||||||
|
api_call() {
|
||||||
|
local method="$1" path="$2" data="${3:-}"
|
||||||
|
local body_file http_code rc=0
|
||||||
|
body_file=$(mktemp)
|
||||||
|
local args=(-sS --retry 3 --retry-delay 2 --retry-all-errors
|
||||||
|
-w '%{http_code}'
|
||||||
|
-o "${body_file}"
|
||||||
|
-X "${method}"
|
||||||
|
-H "Authorization: token ${TOKEN}")
|
||||||
|
if [ -n "${data}" ]; then
|
||||||
|
args+=(-H "Content-Type: application/json" --data "${data}")
|
||||||
|
fi
|
||||||
|
http_code=$(curl "${args[@]}" "${API_URL}${path}" 2>/dev/null) || rc=$?
|
||||||
|
printf '%s|%s\n' "${http_code:-000}" "${rc}"
|
||||||
|
cat "${body_file}"
|
||||||
|
rm -f "${body_file}"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
echo "Creating new next-release branch from ${BASE_BRANCH}..."
|
# Extract first-line meta and remaining body from api_call output.
|
||||||
|
meta_line() { printf '%s\n' "$1" | head -n1; }
|
||||||
|
body_lines() { printf '%s\n' "$1" | tail -n +2; }
|
||||||
|
http_of() { local m; m=$(meta_line "$1"); printf '%s' "${m%%|*}"; }
|
||||||
|
ok_code() { [ -n "$1" ] && [ "$1" -ge 200 ] 2>/dev/null && [ "$1" -lt 400 ]; }
|
||||||
|
|
||||||
# Check if CHANGELOG.md exists on base branch to determine create vs update
|
# Delete existing next-release branch if it exists (auto-closes any open PR)
|
||||||
CHANGELOG_SHA=$(curl -sf --retry 3 --retry-delay 2 --retry-connrefused \
|
echo "Checking for existing next-release branch..."
|
||||||
-H "Authorization: token ${TOKEN}" \
|
OUT=$(api_call GET "/branches/next-release")
|
||||||
"${API_URL}/contents/CHANGELOG.md?ref=${BASE_BRANCH}" | jq -r '.sha // empty')
|
CODE=$(http_of "${OUT}")
|
||||||
|
if [ "${CODE}" = "200" ]; then
|
||||||
if [ -n "${CHANGELOG_SHA}" ]; then
|
echo "Deleting existing next-release branch..."
|
||||||
echo "Updating CHANGELOG.md (exists on ${BASE_BRANCH}) on new branch..."
|
OUT=$(api_call DELETE "/branches/next-release")
|
||||||
RESPONSE=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused -w "\n%{http_code}" -X PUT \
|
echo " delete result: $(meta_line "${OUT}")"
|
||||||
-H "Authorization: token ${TOKEN}" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
--data "$(jq -n \
|
|
||||||
--arg content "${CHANGELOG_CONTENT}" \
|
|
||||||
--arg sha "${CHANGELOG_SHA}" \
|
|
||||||
--arg message "${TITLE}" \
|
|
||||||
--arg branch "${BASE_BRANCH}" \
|
|
||||||
--arg new_branch "next-release" \
|
|
||||||
'{content: $content, sha: $sha, message: $message, branch: $branch, new_branch: $new_branch}')" \
|
|
||||||
"${API_URL}/contents/CHANGELOG.md")
|
|
||||||
else
|
else
|
||||||
echo "Creating CHANGELOG.md on new branch..."
|
echo " no existing branch (HTTP ${CODE})"
|
||||||
RESPONSE=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused -w "\n%{http_code}" -X POST \
|
|
||||||
-H "Authorization: token ${TOKEN}" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
--data "$(jq -n \
|
|
||||||
--arg content "${CHANGELOG_CONTENT}" \
|
|
||||||
--arg message "${TITLE}" \
|
|
||||||
--arg branch "${BASE_BRANCH}" \
|
|
||||||
--arg new_branch "next-release" \
|
|
||||||
'{content: $content, message: $message, branch: $branch, new_branch: $new_branch}')" \
|
|
||||||
"${API_URL}/contents/CHANGELOG.md")
|
|
||||||
fi
|
|
||||||
HTTP_CODE=$(echo "${RESPONSE}" | tail -1)
|
|
||||||
BODY=$(echo "${RESPONSE}" | sed '$d')
|
|
||||||
if [ "${HTTP_CODE}" -ge 400 ]; then
|
|
||||||
echo "Error with CHANGELOG.md: ${BODY}"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Wait for next-release branch to be indexed by Gitea before subsequent writes
|
# Explicitly create next-release branch from base
|
||||||
echo "Waiting for next-release branch to be ready..."
|
echo "Creating next-release branch from ${BASE_BRANCH}..."
|
||||||
|
BRANCH_PAYLOAD=$(jq -n --arg new "next-release" --arg old "${BASE_BRANCH}" \
|
||||||
|
'{new_branch_name: $new, old_branch_name: $old}')
|
||||||
|
for i in $(seq 1 5); do
|
||||||
|
OUT=$(api_call POST "/branches" "${BRANCH_PAYLOAD}")
|
||||||
|
META=$(meta_line "${OUT}"); BODY=$(body_lines "${OUT}"); CODE="${META%%|*}"
|
||||||
|
if ok_code "${CODE}"; then
|
||||||
|
echo "Branch created (${META})"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
if [ "${i}" = "5" ]; then
|
||||||
|
echo "Branch create failed after 5 attempts (${META}): ${BODY}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo " attempt ${i}/5 (${META}): ${BODY} — retrying..."
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
|
||||||
|
# Poll until branch is readable
|
||||||
|
echo "Waiting for branch readiness..."
|
||||||
for i in $(seq 1 10); do
|
for i in $(seq 1 10); do
|
||||||
BRANCH_STATUS=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused \
|
OUT=$(api_call GET "/branches/next-release")
|
||||||
-w "%{http_code}" -o /dev/null \
|
META=$(meta_line "${OUT}"); CODE="${META%%|*}"
|
||||||
-H "Authorization: token ${TOKEN}" \
|
if [ "${CODE}" = "200" ]; then
|
||||||
"${API_URL}/branches/next-release")
|
|
||||||
if [ "${BRANCH_STATUS}" = "200" ]; then
|
|
||||||
echo "Branch ready after ${i} attempt(s)"
|
echo "Branch ready after ${i} attempt(s)"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
if [ "${i}" = "10" ]; then
|
if [ "${i}" = "10" ]; then
|
||||||
echo "Branch next-release not found after 10 attempts, giving up"
|
echo "Branch not ready after 10 attempts (last: ${META})"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Branch not ready yet (attempt ${i}/10), waiting..."
|
echo " attempt ${i}/10 (${META}) — waiting..."
|
||||||
sleep 3
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
# Check if .version exists on base branch
|
# Fetch file blob SHAs from next-release (inherited from base on creation)
|
||||||
VERSION_SHA=$(curl -sf --retry 3 --retry-delay 2 --retry-connrefused \
|
fetch_sha() {
|
||||||
-H "Authorization: token ${TOKEN}" \
|
local path="$1" out meta code body
|
||||||
"${API_URL}/contents/.version?ref=${BASE_BRANCH}" | jq -r '.sha // empty')
|
out=$(api_call GET "/contents/${path}?ref=next-release")
|
||||||
|
meta=$(meta_line "${out}"); code="${meta%%|*}"; body=$(body_lines "${out}")
|
||||||
if [ -n "${VERSION_SHA}" ]; then
|
if [ "${code}" = "200" ]; then
|
||||||
echo "Updating .version on next-release branch..."
|
printf '%s' "${body}" | jq -r '.sha // empty'
|
||||||
VERSION_PAYLOAD=$(jq -n \
|
|
||||||
--arg content "${VERSION_CONTENT}" \
|
|
||||||
--arg sha "${VERSION_SHA}" \
|
|
||||||
--arg message "${TITLE}" \
|
|
||||||
--arg branch "next-release" \
|
|
||||||
'{content: $content, sha: $sha, message: $message, branch: $branch}')
|
|
||||||
VERSION_METHOD=PUT
|
|
||||||
else
|
|
||||||
echo "Creating .version on next-release branch..."
|
|
||||||
VERSION_PAYLOAD=$(jq -n \
|
|
||||||
--arg content "${VERSION_CONTENT}" \
|
|
||||||
--arg message "${TITLE}" \
|
|
||||||
--arg branch "next-release" \
|
|
||||||
'{content: $content, message: $message, branch: $branch}')
|
|
||||||
VERSION_METHOD=POST
|
|
||||||
fi
|
|
||||||
|
|
||||||
for i in $(seq 1 5); do
|
|
||||||
RESPONSE=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused \
|
|
||||||
-w "\n%{http_code}" -X "${VERSION_METHOD}" \
|
|
||||||
-H "Authorization: token ${TOKEN}" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
--data "${VERSION_PAYLOAD}" \
|
|
||||||
"${API_URL}/contents/.version")
|
|
||||||
HTTP_CODE=$(echo "${RESPONSE}" | tail -1)
|
|
||||||
BODY=$(echo "${RESPONSE}" | sed '$d')
|
|
||||||
if [ "${HTTP_CODE}" -lt 400 ]; then
|
|
||||||
echo ".version write succeeded"
|
|
||||||
break
|
|
||||||
fi
|
fi
|
||||||
if [ "${i}" = "5" ]; then
|
}
|
||||||
echo "Error writing .version after 5 attempts (HTTP ${HTTP_CODE}): ${BODY}"
|
CHANGELOG_SHA=$(fetch_sha "CHANGELOG.md")
|
||||||
exit 1
|
VERSION_SHA=$(fetch_sha ".version")
|
||||||
|
|
||||||
|
# Write file with retry. Args: PATH CONTENT_B64 [SHA]
|
||||||
|
write_file() {
|
||||||
|
local path="$1" content="$2" sha="${3:-}"
|
||||||
|
local method payload out meta body code
|
||||||
|
if [ -n "${sha}" ]; then
|
||||||
|
method=PUT
|
||||||
|
payload=$(jq -n \
|
||||||
|
--arg content "${content}" \
|
||||||
|
--arg sha "${sha}" \
|
||||||
|
--arg message "${TITLE}" \
|
||||||
|
--arg branch "next-release" \
|
||||||
|
'{content: $content, sha: $sha, message: $message, branch: $branch}')
|
||||||
|
else
|
||||||
|
method=POST
|
||||||
|
payload=$(jq -n \
|
||||||
|
--arg content "${content}" \
|
||||||
|
--arg message "${TITLE}" \
|
||||||
|
--arg branch "next-release" \
|
||||||
|
'{content: $content, message: $message, branch: $branch}')
|
||||||
fi
|
fi
|
||||||
echo ".version write attempt ${i}/5 failed (HTTP ${HTTP_CODE}): ${BODY} — retrying..."
|
for i in $(seq 1 5); do
|
||||||
sleep 3
|
out=$(api_call "${method}" "/contents/${path}" "${payload}")
|
||||||
done
|
meta=$(meta_line "${out}"); body=$(body_lines "${out}"); code="${meta%%|*}"
|
||||||
|
if ok_code "${code}"; then
|
||||||
|
echo "${path} write succeeded (${meta})"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if [ "${i}" = "5" ]; then
|
||||||
|
echo "${path} write failed after 5 attempts (${meta}): ${body}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo " ${path} attempt ${i}/5 (${meta}): ${body} — retrying..."
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
echo "Creating new PR..."
|
echo "Writing CHANGELOG.md to next-release..."
|
||||||
|
write_file "CHANGELOG.md" "${CHANGELOG_CONTENT}" "${CHANGELOG_SHA}"
|
||||||
|
echo "Writing .version to next-release..."
|
||||||
|
write_file ".version" "${VERSION_CONTENT}" "${VERSION_SHA}"
|
||||||
|
|
||||||
|
# Create PR
|
||||||
|
echo "Creating PR..."
|
||||||
PR_DATA=$(jq -n \
|
PR_DATA=$(jq -n \
|
||||||
--arg title "${TITLE}" \
|
--arg title "${TITLE}" \
|
||||||
--arg body "${DESCRIPTION}" \
|
--arg body "${DESCRIPTION}" \
|
||||||
--arg head "next-release" \
|
--arg head "next-release" \
|
||||||
--arg base "${BASE_BRANCH}" \
|
--arg base "${BASE_BRANCH}" \
|
||||||
'{title: $title, body: $body, head: $head, base: $base}')
|
'{title: $title, body: $body, head: $head, base: $base}')
|
||||||
|
|
||||||
for i in $(seq 1 5); do
|
for i in $(seq 1 5); do
|
||||||
RESPONSE=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused \
|
OUT=$(api_call POST "/pulls" "${PR_DATA}")
|
||||||
-w "\n%{http_code}" -X POST \
|
META=$(meta_line "${OUT}"); BODY=$(body_lines "${OUT}"); CODE="${META%%|*}"
|
||||||
-H "Authorization: token ${TOKEN}" \
|
if ok_code "${CODE}"; then
|
||||||
-H "Content-Type: application/json" \
|
echo "PR created (${META})"
|
||||||
--data "${PR_DATA}" \
|
|
||||||
"${API_URL}/pulls")
|
|
||||||
HTTP_CODE=$(echo "${RESPONSE}" | tail -1)
|
|
||||||
BODY=$(echo "${RESPONSE}" | sed '$d')
|
|
||||||
if [ "${HTTP_CODE}" -lt 400 ]; then
|
|
||||||
echo "PR created successfully"
|
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
if [ "${i}" = "5" ]; then
|
if [ "${i}" = "5" ]; then
|
||||||
echo "Error creating PR after 5 attempts (HTTP ${HTTP_CODE}): ${BODY}"
|
echo "PR creation failed after 5 attempts (${META}): ${BODY}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "PR creation attempt ${i}/5 failed (HTTP ${HTTP_CODE}), retrying..."
|
echo " PR attempt ${i}/5 (${META}): ${BODY} — retrying..."
|
||||||
sleep 3
|
sleep 3
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user