#!/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/PilgrimsOfKatan # # 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/PilgrimsOfKatan}}" 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="PilgrimsOfKatan-${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; } 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 echo "publish-release: ${tag} -> release ${id} on ${api#*/repos/}, asset ${apk_name}"