Extract release JSON building into helper script (avoid YAML/bash heredoc trap)

This commit is contained in:
Keith Solomon
2026-08-11 13:41:38 -05:00
parent 123c0dbb7b
commit 3bae89a203
2 changed files with 86 additions and 54 deletions
+8 -54
View File
@@ -76,17 +76,12 @@ jobs:
REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}
GH_API_URL: ${{ github.api_url }}
GH_SHA: ${{ github.sha }}
run: |
set -euo pipefail
# Branch on host: github.com sets GITHUB_API_URL; Gitea leaves it empty
# (or set to the Gitea base, which we detect by path). We treat any
# api_url that is the github.com default as GitHub and anything else as Gitea.
zip_path=( dist/projects-portfolio-*.zip )
zip_path="${zip_path[0]}"
asset_name=$(basename "$zip_path")
asset_bytes=$(wc -c < "$zip_path")
if [[ "$GH_API_URL" == "https://api.github.com" ]]; then
# ---- GitHub.com path ----
@@ -94,35 +89,16 @@ jobs:
echo "::error::GITHUB_TOKEN is not available on this runner. Check repo permissions." >&2
exit 1
fi
# Build the JSON payload to a temp file via Python heredoc.
python3 - > /tmp/release-payload.json <<'PYEOF'
import json, os
print(json.dumps({
"tag_name": os.environ["TAG"],
"name": os.environ["TAG"],
"body": (
"Automated release.\n\n"
"Built from commit " + os.environ.get("GH_SHA", "unknown") + ".\n\n"
"See the workflow run for the artifact."
),
"draft": False,
"prerelease": False,
"generate_release_notes": True,
}))
PYEOF
payload=$(python3 scripts/release-helper.py build-payload --github)
release_json=$(curl -fsS -X POST \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/json" \
-d @/tmp/release-payload.json \
-d "$payload" \
"${GH_API_URL}/repos/${REPO}/releases")
release_id=$(python3 -c 'import json,sys; print(json.loads(sys.stdin.read())["id"])' <<<"$release_json")
upload_url=$(python3 -c 'import json,sys; print(json.loads(sys.stdin.read())["upload_url"])' <<<"$release_json")
release_id=$(echo "$release_json" | python3 scripts/release-helper.py extract-id)
upload_url=$(echo "$release_json" | python3 scripts/release-helper.py extract-upload-url)
echo "Created GitHub release id=$release_id"
# Asset upload uses the per-release upload_url (template with {?name,label}).
curl -fsS -X POST \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
@@ -136,41 +112,19 @@ jobs:
echo "::error::GITEA_TOKEN secret is not set. Create a personal access token in Gitea with 'write:repository' scope and add it as a repository secret named GITEA_TOKEN." >&2
exit 1
fi
# Gitea's api_url may be unset or set to the Gitea base.
if [ -z "${GH_API_URL:-}" ] || [[ "$GH_API_URL" == "https://git.keithsolomon.net"* ]]; then
API="https://git.keithsolomon.net/api/v1"
else
API="${GH_API_URL%/}/api/v1"
fi
python3 - > /tmp/release-payload.json <<'PYEOF'
import json, os
print(json.dumps({
"tag_name": os.environ["TAG"],
"name": os.environ["TAG"],
"body": (
"Automated release.\n\n"
"Built from commit " + os.environ.get("GH_SHA", "unknown") + ".\n\n"
"See the workflow run for the artifact."
),
"draft": False,
"prerelease": False,
}))
PYEOF
payload=$(python3 scripts/release-helper.py build-payload)
release_json=$(curl -fsS -X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d @/tmp/release-payload.json \
-d "$payload" \
"${API}/repos/${REPO}/releases")
release_id=$(python3 -c 'import json,sys; print(json.loads(sys.stdin.read())["id"])' <<<"$release_json")
if [ -z "$release_id" ]; then
echo "::error::Failed to create Gitea release. Response: $release_json" >&2
exit 1
fi
release_id=$(echo "$release_json" | python3 scripts/release-helper.py extract-id)
echo "Created Gitea release id=$release_id"
curl -fsS -X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/zip" \