#!/bin/sh # Build and publish the next t3code Android APK release. # # Without flags: build and publish the next APK version. # With --dry-run: calculate and print the next version without building or publishing. set -eu ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) DRY_RUN=0 for arg in "$@"; do case "$arg" in --dry-run) DRY_RUN=1 ;; --help|-h) printf '%s\n' 'Usage: ./scripts/release-apk.sh [--dry-run]' printf '%s\n' ' (default) build and publish the next APK release' printf '%s\n' ' --dry-run calculate and print the next version without building or publishing' exit 0 ;; *) echo "Unknown option: $arg" >&2; exit 2 ;; esac done CONFIG_FILE=${CONFIG_FILE:-$ROOT/.env.release} case "$CONFIG_FILE" in /*) ;; *) CONFIG_FILE="$ROOT/$CONFIG_FILE" ;; esac if [ -f "$CONFIG_FILE" ]; then set -a # shellcheck disable=SC1090 . "$CONFIG_FILE" set +a fi API=${GITEA_API_URL:-https://git.molberg.cloud/api/v1/repos/alex/t3code} REPOSITORY_URL=${T3CODE_APK_REPOSITORY_URL:-https://git.molberg.cloud/alex/t3code} TOKEN=${APK_RELEASE_TOKEN:-${GITEA_TOKEN:-}} if [ "$DRY_RUN" -eq 0 ] && [ -z "$TOKEN" ]; then echo "Set GITEA_TOKEN or APK_RELEASE_TOKEN in $CONFIG_FILE or the environment" >&2 exit 2 fi RELEASES_JSON=$(mktemp) trap 'rm -f "$RELEASES_JSON"' EXIT HUP INT TERM if [ -n "$TOKEN" ]; then curl -fsS -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' \ -H "Authorization: token $TOKEN" "$API/releases?limit=50" > "$RELEASES_JSON" else curl -fsS -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' \ "$API/releases?limit=50" > "$RELEASES_JSON" fi VERSION_CODE=$(python3 - "$RELEASES_JSON" "${APK_VERSION_CODE:-}" <<'PY' import json import re import sys path, override = sys.argv[1:] if override: print(override) raise SystemExit highest = 0 with open(path, encoding="utf-8") as handle: for release in json.load(handle): tag = str(release.get("tag_name", "")) match = re.fullmatch(r"apk-([1-9][0-9]*)", tag) if match: highest = max(highest, int(match.group(1))) print(highest + 1) PY ) case "$VERSION_CODE" in *[!0-9]*|'') echo "APK_VERSION_CODE must be a positive integer" >&2; exit 2 ;; esac [ "$VERSION_CODE" -gt 0 ] || { echo "APK_VERSION_CODE must be positive" >&2; exit 2; } [ "$VERSION_CODE" -le 2100000000 ] || { echo "APK_VERSION_CODE must be at most 2100000000" >&2; exit 2; } TAG="apk-$VERSION_CODE" VERSION_NAME=$(sed -n 's/.*version: "\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' "$ROOT/apps/mobile/app.config.ts" | head -n 1) VERSION_NAME=${VERSION_NAME:-unknown} printf 'Next APK release: %s (versionName %s)\n' "$TAG" "$VERSION_NAME" [ "$DRY_RUN" -eq 0 ] || exit 0 export T3CODE_APK_RELEASE_TAG="$TAG" export T3CODE_APK_RELEASE_COMMIT=$(git -C "$ROOT" rev-parse HEAD) export T3CODE_APK_REPOSITORY_URL="$REPOSITORY_URL" export APK_RELEASE_TOKEN="$TOKEN" export APP_VARIANT=production export T3CODE_MOBILE_UPDATES_ENABLED=0 cd "$ROOT" bash scripts/build-fork-apk.sh APK="release/t3code-$TAG.apk" CHECKSUM="$APK.sha256" [ -s "$APK" ] || { echo "Build did not produce $APK" >&2; exit 1; } [ -s "$CHECKSUM" ] || { echo "Build did not produce $CHECKSUM" >&2; exit 1; } # build-fork-apk.sh runs `sha256sum` from inside release/, so the file references # only the bare filename. Verify from that directory. ( cd release sha256sum -c "$(basename "$CHECKSUM")" ) if [ -z "${APK_RELEASE_BODY:-}" ]; then APK_RELEASE_BODY=$(python3 - "$TAG" "$VERSION_CODE" "$VERSION_NAME" "$T3CODE_APK_RELEASE_COMMIT" "$(sha256sum "$APK" | cut -d' ' -f1)" <<'PY' import json import sys print(json.dumps({ "schema": 1, "channel": "production", "versionCode": int(sys.argv[2]), "versionName": sys.argv[3], "branch": "main", "commit": sys.argv[4], "sha256": sys.argv[5], }, separators=(",", ":"))) PY ) fi APK_RELEASE_BODY="$APK_RELEASE_BODY" node --input-type=module <<'NODE' import * as NodeFS from "node:fs/promises"; import { publishForkApk } from "./scripts/publish-fork-apk.ts"; const tag = process.env.T3CODE_APK_RELEASE_TAG; const repositoryUrl = process.env.T3CODE_APK_REPOSITORY_URL; const token = process.env.APK_RELEASE_TOKEN; const commit = process.env.T3CODE_APK_RELEASE_COMMIT; if (!tag || !repositoryUrl || !token || !commit) throw new Error("Release environment is incomplete."); await publishForkApk({ tag, repositoryUrl, token, commit, body: process.env.APK_RELEASE_BODY, apk: new Blob([await NodeFS.readFile(`release/t3code-${tag}.apk`)], { type: "application/vnd.android.package-archive" }), checksum: new Blob([await NodeFS.readFile(`release/t3code-${tag}.apk.sha256`)], { type: "text/plain" }), }); NODE printf 'Published %s\n' "$TAG"