diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 22c3b64..53c96bd 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -110,24 +110,37 @@ jobs: tag="${GITHUB_REF_NAME}" api="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" - # The release usually exists already, created by hand with its notes. - id=$(curl -sS -H "Authorization: token $TOKEN" "$api/releases/tags/$tag" \ - | sed -n 's/.*"id":\([0-9]*\).*/\1/p' | head -1) + # Gitea materialises a release row for every pushed tag, so this + # nearly always finds one and the create below is for a tag pushed + # some other way. + # + # The id is read with grep rather than sed because sed's .* is greedy: + # "id" appears again inside the nested author object, so the old + # pattern took the author's id when it matched at all — and it did not + # match "id": 13 with a space, which is what actually happened. The + # first "id" in a release object is the release's own. + release_id() { + curl -sS -H "Authorization: token $TOKEN" "$api/releases/tags/$tag" \ + | grep -o '"id"[[:space:]]*:[[:space:]]*[0-9]*' \ + | head -1 | grep -o '[0-9]*$' + } + + id=$(release_id || true) if [ -z "$id" ]; then - echo "creating release $tag" - body=$(curl -sS -w '\n%{http_code}' -X POST \ + echo "no release for $tag yet; creating one" + out=$(curl -sS -w '\n%{http_code}' -X POST \ -H "Authorization: token $TOKEN" \ -H "Content-Type: application/json" \ -d "{\"tag_name\":\"$tag\",\"name\":\"$tag\"}" \ "$api/releases") - code=$(printf '%s' "$body" | tail -1) - echo "create returned $code" - id=$(printf '%s' "$body" | sed -n 's/.*"id":\([0-9]*\).*/\1/p' | head -1) - # Whatever went wrong, the server said so, and the previous version - # of this step threw that away and reported only that it had failed. - [ -n "$id" ] || printf '%s\n' "$body" | head -5 >&2 + echo "create returned $(printf '%s' "$out" | tail -1)" + # Re-read rather than parsing the create response: a 409 means it + # was there all along, which is a success for our purposes. + id=$(release_id || true) + [ -n "$id" ] || printf '%s\n' "$out" | head -5 >&2 fi [ -n "$id" ] || { echo "could not find or create the release" >&2; exit 1; } + echo "release id $id" for f in dist/*; do echo "uploading $(basename "$f")"