diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 6569c8a..596bb58 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -146,30 +146,55 @@ jobs: exit 1 fi - release_id=$(curl -sf -H "Authorization: token $TOKEN" \ - "$API/releases/tags/$GITHUB_REF_NAME" \ - | node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).id" 2>/dev/null || true) + # Report the HTTP status and body on failure. A bare `curl -sf | JSON.parse` + # dies with "Unexpected end of JSON input" and hides the real cause, which is + # 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 - 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" \ -d "{\"tag_name\":\"$GITHUB_REF_NAME\",\"name\":\"$GITHUB_REF_NAME\"}" \ - "$API/releases" \ - | node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).id") + | node -pe "JSON.parse(require('fs').readFileSync(0,'utf8')).id") 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 - 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") - echo "Attaching $name" - curl -sf -X POST -H "Authorization: token $TOKEN" \ - -F "attachment=@$f" \ - "$API/releases/$release_id/assets?name=$name" -o /dev/null + echo "Attaching $name ($(du -h "$f" | cut -f1))" + api POST "/releases/$release_id/assets?name=$name" -F "attachment=@$f" >/dev/null 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 - 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 fi echo "Attached $found artifacts to release $release_id"