Merge pull request 'Surface API errors in the publish step' (#5) from ci/harden-publish into main

This commit was merged in pull request #5.
This commit is contained in:
2026-08-29 03:39:57 +00:00
+38 -13
View File
@@ -146,30 +146,55 @@ jobs:
exit 1 exit 1
fi fi
release_id=$(curl -sf -H "Authorization: token $TOKEN" \ # Report the HTTP status and body on failure. A bare `curl -sf | JSON.parse`
"$API/releases/tags/$GITHUB_REF_NAME" \ # dies with "Unexpected end of JSON input" and hides the real cause, which is
| node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).id" 2>/dev/null || true) # how the v1.0.0 run failed on a missing token.
api() {
local method="$1" path="$2"; shift 2
local out code
out=$(curl -s -w '\n%{http_code}' -X "$method" \
-H "Authorization: token $TOKEN" "$API$path" "$@")
code=$(printf '%s' "$out" | tail -n1)
body=$(printf '%s' "$out" | sed '$d')
if [ "$code" -ge 400 ]; then
echo "$method $path -> HTTP $code" >&2
echo "$body" >&2
return 1
fi
printf '%s' "$body"
}
# Reuse the release if the tag already has one, otherwise create it.
release_id=$(api GET "/releases/tags/$GITHUB_REF_NAME" 2>/dev/null \
| node -pe "JSON.parse(require('fs').readFileSync(0,'utf8')).id" 2>/dev/null || true)
if [ -z "${release_id:-}" ]; then if [ -z "${release_id:-}" ]; then
release_id=$(curl -sf -X POST -H "Authorization: token $TOKEN" \ echo "No release for $GITHUB_REF_NAME yet; creating it."
release_id=$(api POST "/releases" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "{\"tag_name\":\"$GITHUB_REF_NAME\",\"name\":\"$GITHUB_REF_NAME\"}" \ -d "{\"tag_name\":\"$GITHUB_REF_NAME\",\"name\":\"$GITHUB_REF_NAME\"}" \
"$API/releases" \ | node -pe "JSON.parse(require('fs').readFileSync(0,'utf8')).id")
| node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).id")
fi fi
if [ -z "${release_id:-}" ]; then
echo "Could not determine a release id." >&2
exit 1
fi
echo "Publishing to release $release_id"
# while-read rather than word-splitting a find, so spaces in names are safe.
found=0 found=0
for f in $(find artifacts -type f \( -name '*.AppImage' -o -name '*.zip' -o -name '*.dmg' -o -name '*.exe' \)); do while IFS= read -r f; do
name=$(basename "$f") name=$(basename "$f")
echo "Attaching $name" echo "Attaching $name ($(du -h "$f" | cut -f1))"
curl -sf -X POST -H "Authorization: token $TOKEN" \ api POST "/releases/$release_id/assets?name=$name" -F "attachment=@$f" >/dev/null
-F "attachment=@$f" \
"$API/releases/$release_id/assets?name=$name" -o /dev/null
found=$((found + 1)) found=$((found + 1))
done done < <(find artifacts -type f \( -name '*.AppImage' -o -name '*.zip' \
-o -name '*.dmg' -o -name '*.exe' \) | sort)
if [ "$found" -eq 0 ]; then if [ "$found" -eq 0 ]; then
echo "No artifacts found to attach." >&2 echo "No artifacts found to attach; build jobs produced nothing." >&2
find artifacts -type f | head -20 >&2
exit 1 exit 1
fi fi
echo "Attached $found artifacts to release $release_id" echo "Attached $found artifacts to release $release_id"