#!/bin/sh # Publish a built APK to this repository's Gitea releases — the channel the # in-app updater reads (docs/CI.md). # # The workflows call this, and so can a person: it is the whole release step, # runnable by hand on any machine with curl, which matters because a Gitea # Actions runner has to exist before the workflows can run at all. # # scripts/publish-release.sh [target-sha] # # Environment: # GITEA_TOKEN required — a token that may create releases on the repo. # GITEA_API API base, default https://git.wallacenet.ca/api/v1 # GITEA_REPO owner/name, default devadmin/ProLibertateGames # # The four arguments can also arrive as RELEASE_TAG, RELEASE_APK, # RELEASE_KIND and RELEASE_TARGET, which is how the workflows pass them. set -eu usage() { sed -n '2,17p' "$0" | sed 's/^# \{0,1\}//' >&2 } tag="${1:-${RELEASE_TAG:-}}" apk="${2:-${RELEASE_APK:-}}" kind="${3:-${RELEASE_KIND:-}}" target="${4:-${RELEASE_TARGET:-${GITHUB_SHA:-HEAD}}}" case "$kind" in prerelease|release) ;; *) usage; exit 2 ;; esac [ -n "$tag" ] || { usage; exit 2; } [ -f "$apk" ] || { echo "publish-release: no such APK: ${apk:-}" >&2; exit 2; } [ -n "${GITEA_TOKEN:-}" ] || { echo "publish-release: GITEA_TOKEN is not set" >&2; exit 2; } api="${GITEA_API:-${GITHUB_API_URL:-https://git.wallacenet.ca/api/v1}}/repos/${GITEA_REPO:-${GITHUB_REPOSITORY:-devadmin/ProLibertateGames}}" tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT INT TERM # Named for its tag, not for whatever the build happened to call it: the # updater picks the versioned APK out of a release's assets by finding the one # whose name contains the tag, so an asset called app-debug.apk would only be # found by the fallback rule. apk_name="ProLibertateGames-${tag}.apk" # The tag and the file name are both derived from gradle.properties and a run # number, so neither can carry a quote or a backslash into this JSON. if [ "$kind" = "prerelease" ]; then prerelease=true note="Automated dev-channel build of ${target}. Newer than production and less tested — switch to the Dev update channel in Settings to be offered these. Published as a prerelease so production-channel installs never see it." else prerelease=false note="Production-channel build of ${target}. Download ${apk_name} below and install it on an Android device (you may need to allow installs from unknown apps). It is signed with the repo's fixed keystore, which is what lets the in-app updater install over it." fi cat > "$tmp/release.json" <&2 cat "$tmp/create.json" >&2 echo >&2 exit 1 fi # Gitea answers compact JSON, and the release's own id is the first "id" in # it — the author's object comes later in the document. id="$(grep -o '"id":[0-9]*' "$tmp/create.json" | head -n1 | tr -dc '0-9' || true)" [ -n "$id" ] || { echo "publish-release: no release id in response" >&2; cat "$tmp/create.json" >&2; echo >&2; exit 1; } # Attach the APK first, then check the stored state: uploading has been # observed to leave a freshly created release as a draft, whatever the create # response said, so the check has to be the last thing that happens rather # than a step in the middle. code="$(curl -sS -o "$tmp/asset.json" -w '%{http_code}' -X POST \ -H "Authorization: token ${GITEA_TOKEN}" \ -F "attachment=@${apk};filename=${apk_name}" \ "$api/releases/${id}/assets")" if [ "$code" != "201" ] && [ "$code" != "200" ]; then echo "publish-release: attaching ${apk_name} to release ${id} failed (HTTP ${code})" >&2 cat "$tmp/asset.json" >&2 echo >&2 exit 1 fi # Read back what the database actually stored before declaring success. A draft # is invisible to the updater — exactly what a channel must not silently become # — and this instance has been seen to report one as published at create time # and store it as a draft by the time it is read. curl -sS -o "$tmp/stored.json" -H "Authorization: token ${GITEA_TOKEN}" "$api/releases/${id}" if grep -q '"draft":true' "$tmp/stored.json" || ! grep -q "\"prerelease\":${prerelease}" "$tmp/stored.json"; then state="$(curl -sS -o "$tmp/fix.json" -w '%{http_code}' -X PATCH \ -H "Authorization: token ${GITEA_TOKEN}" \ -H "Content-Type: application/json" \ -d "{\"draft\":false,\"prerelease\":${prerelease}}" \ "$api/releases/${id}")" if [ "$state" != "200" ]; then echo "publish-release: release ${tag} is stored with the wrong state (HTTP ${state})" >&2 cat "$tmp/fix.json" >&2 echo >&2 exit 1 fi echo "publish-release: corrected the stored state of ${tag} (it was saved as a draft)" fi echo "publish-release: ${tag} -> release ${id} on ${api#*/repos/}, asset ${apk_name}"