build: sign releases with the permanent key & add the release script (#49)

Two things a self-updating app cannot get wrong: the APK must be signed by
the key the TV will accept forever, and the thing published must be the
thing that was built.

The release build now reads keystore.properties (gitignored) and signs
with jtube-release.jks — RSA 4096, SHA384, alias jtube, valid to 2054 —
falling back to the debug key only when that file is absent, so a fresh
clone still builds. The key itself never enters git; it lives in the
encrypted agent secret store (passwords + an encrypted copy of the
keystore, verified byte-identical on decrypt) and belongs in KeePassXC
as a file attachment (docs/releases.md §4).

scripts/release.sh publishes: it refuses an untagged HEAD, a missing
keystore, an APK whose own versionCode/versionName disagree with
version.properties (stale build), a versionCode not greater than the
newest published release, and then writes the description with the
"versionCode: N" line the update check reads, plus a changelog from the
commits since the previous tag (or --notes FILE), and uploads the APK as
the release's asset. --dry-run prints the description without publishing.
Refs #49
This commit is contained in:
Jeena 2026-10-31 00:03:40 +00:00
parent c73749f4fb
commit 93b7b4eb9a
4 changed files with 168 additions and 4 deletions

4
.gitignore vendored
View file

@ -10,3 +10,7 @@
.cxx
reports/*-shots/
reports/*.md
# Release signing material — never in git (see docs/releases.md)
/keystore.properties
/jtube-release.jks

View file

@ -33,6 +33,15 @@ android {
// filter is clamped to "verify"), so a debug build runs interpreted and the
// TV sits at ~3fps with Compose. This variant is fast enough to develop
// against and keeps class/method names for profiling.
// Release signing: keystore.properties (gitignored) when present, so the
// TV build is signed by the real release key and can update itself. A
// checkout without it still builds — unsigned release, debug-signed bench
// — which is what CI/first-time setups see.
val keystoreProps = Properties()
val keystoreFile = rootProject.file("keystore.properties")
if (keystoreFile.exists()) keystoreFile.inputStream().use { keystoreProps.load(it) }
val hasReleaseKey = keystoreProps.getProperty("storeFile") != null
create("bench") {
initWith(getByName("release"))
isMinifyEnabled = false
@ -46,7 +55,16 @@ android {
// at ~2fps (dumpsys gfxinfo: 87% janky, UI-thread stalls up to 4s),
// while the GPU is fine. Same signature keeps install -r seamless.
isMinifyEnabled = true
signingConfig = signingConfigs.getByName("debug")
signingConfig = if (hasReleaseKey) {
signingConfigs.create("release") {
storeFile = rootProject.file(keystoreProps.getProperty("storeFile"))
storePassword = keystoreProps.getProperty("storePassword")
keyAlias = keystoreProps.getProperty("keyAlias")
keyPassword = keystoreProps.getProperty("keyPassword")
}
} else {
signingConfigs.getByName("debug")
}
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"

View file

@ -144,12 +144,21 @@ Releases must be signed with the **same key forever**: Android refuses to instal
an update signed by a different key, and the app also verifies the downloaded
APK's signer before handing it to the installer.
- Keystore: `jtube-release.jks` — **not** in git. Backed up in KeePassXC (the
file as an attachment, passwords in the same entry).
- Keystore: `jtube-release.jks` in the repo root — **not** in git. Backups, in
order of preference:
1. KeePassXC: the file as an attachment to the entry that holds the passwords;
2. the agent secret store (encrypted, Syncthing-synced):
`~/.agents/secrets/jtube-release.jks.sops`, with the passwords in
`~/.agents/secrets/jtube-release.env`
(`JTUBE_RELEASE_KEYSTORE_PASS`, `JTUBE_RELEASE_KEY_PASS`,
`JTUBE_RELEASE_KEY_ALIAS`).
Both were created 2026-10-31 with: RSA 4096, SHA384withRSA, alias `jtube`,
valid until 2054.
- `keystore.properties` at the repo root (gitignored):
```properties
storeFile=../jtube-release.jks
storeFile=jtube-release.jks
storePassword=…
keyAlias=jtube
keyPassword=…

133
scripts/release.sh Executable file
View file

@ -0,0 +1,133 @@
#!/usr/bin/env bash
# Publishes a release to Forgejo: build, verify, then create the release and
# upload the APK. See docs/releases.md for the contract this enforces.
#
# ./scripts/release.sh [--dry-run] [--notes FILE]
#
# Checks that must pass, in this order (each one has bitten a self-updater
# somewhere):
# 1. the checked-out tag is v<versionName> from version.properties
# 2. keystore.properties exists (an unsigned release cannot be installed over
# an existing install)
# 3. the BUILT APK's own versionCode/versionName agree with version.properties
# (a stale APK published under a new tag strands every client)
# 4. its versionCode is greater than the newest published release's
# 5. the release gets a description carrying "versionCode: N" — the line the
# app's update check reads — and the APK as its only asset
set -euo pipefail
cd "$(dirname "$0")/.."
DRY_RUN=0
NOTES_FILE=""
while [ $# -gt 0 ]; do
case "$1" in
--dry-run) DRY_RUN=1 ;;
--notes) shift; NOTES_FILE="${1:-}" ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
shift
done
REPO="jeena/jtube-tv"
SERVER="${JTUBE_RELEASE_SERVER:-https://git.jeena.net}"
SDK="${ANDROID_SDK_ROOT:-$HOME/Android/Sdk}"
AAPT2="$(ls -d "$SDK"/build-tools/*/ 2>/dev/null | tail -1)aapt2"
[ -x "$AAPT2" ] || { echo "aapt2 not found under $SDK/build-tools — set ANDROID_SDK_ROOT" >&2; exit 1; }
fail() { echo "release: $*" >&2; exit 1; }
# --- 1. version + tag ------------------------------------------------------
[ -f version.properties ] || fail "version.properties missing"
VERSION_NAME=$(sed -n 's/^versionName=//p' version.properties | tr -d '[:space:]')
VERSION_CODE=$(sed -n 's/^versionCode=//p' version.properties | tr -d '[:space:]')
TAG="v$VERSION_NAME"
[ -n "$VERSION_NAME" ] && [ -n "$VERSION_CODE" ] || fail "version.properties needs versionName and versionCode"
HEAD_TAG=$(git tag --points-at HEAD | grep -x "$TAG" || true)
[ -n "$HEAD_TAG" ] || fail "HEAD is not tagged $TAG — tag the release commit first:
git tag $TAG && git push origin $TAG"
echo "release: $TAG (versionCode $VERSION_CODE)"
# --- 2. signing key --------------------------------------------------------
[ -f keystore.properties ] || fail "keystore.properties missing — a release must be signed
with the permanent key (docs/releases.md §4)"
# --- 3. build and read the artifact's own truth ----------------------------
./gradlew :app:assembleRelease --quiet
APK=app/build/outputs/apk/release/app-release.apk
[ -f "$APK" ] || fail "no APK at $APK"
BADGING=$("$AAPT2" dump badging "$APK" | head -1)
APK_CODE=$(printf '%s' "$BADGING" | sed -n "s/.*versionCode='\([0-9]*\)'.*/\1/p")
APK_NAME=$(printf '%s' "$BADGING" | sed -n "s/.*versionName='\([^']*\)'.*/\1/p")
[ "$APK_CODE" = "$VERSION_CODE" ] || fail "APK has versionCode $APK_CODE but version.properties says $VERSION_CODE — stale or unbuilt APK"
[ "$APK_NAME" = "$VERSION_NAME" ] || fail "APK has versionName $APK_NAME but version.properties says $VERSION_NAME"
echo "release: built APK matches $TAG"
# --- 4. must be newer than what is published -------------------------------
API="$SERVER/api/v1/repos/$REPO"
LATEST=$(curl -fsS "$API/releases?limit=5" | python3 -c '
import json,sys,re
releases = json.load(sys.stdin)
codes = []
for r in releases:
if r.get("draft"):
continue
m = re.search(r"(?im)^\s*versioncode\s*[:=]\s*(\d+)", r.get("body") or "")
if m: codes.append((int(m.group(1)), r.get("tag_name")))
print(max(codes)[0] if codes else 0)
') || fail "could not read the release list from $API"
if [ "$VERSION_CODE" -le "$LATEST" ]; then
fail "versionCode $VERSION_CODE is not greater than the newest published one ($LATEST) —
bump versionCode in version.properties"
fi
echo "release: versionCode $VERSION_CODE > published $LATEST"
# --- 5. description + publish ---------------------------------------------
BODY="$(mktemp)"
trap 'rm -f "$BODY"' EXIT
{
echo "versionCode: $VERSION_CODE"
echo
if [ -n "$NOTES_FILE" ]; then
cat "$NOTES_FILE"
else
# Default changelog: the commits since the previous tag, one bullet each.
PREV=$(git describe --tags --abbrev=0 "$TAG^" 2>/dev/null || true)
echo "## Changelog"
if [ -n "$PREV" ]; then
git log --no-merges --pretty='- %s' "$PREV..$TAG"
else
git log --no-merges --pretty='- %s' "$TAG"
fi
fi
} > "$BODY"
if [ "$DRY_RUN" = 1 ]; then
echo "release: --dry-run, not publishing. Description:"
sed 's/^/ /' "$BODY"
exit 0
fi
[ -n "${FORGEJO_TOKEN:-}" ] || fail "FORGEJO_TOKEN is not set (see docs/releases.md §3)"
RESP=$(curl -fsS -X POST "$API/releases" \
-H "Authorization: token $FORGEJO_TOKEN" \
-H 'Content-Type: application/json' \
-d "$(python3 -c '
import json,sys
print(json.dumps({"tag_name": sys.argv[1], "name": sys.argv[2],
"body": open(sys.argv[3]).read(), "draft": False, "prerelease": False}))
' "$TAG" "$VERSION_NAME" "$BODY")") || fail "creating the release failed"
RELEASE_ID=$(printf '%s' "$RESP" | python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])')
echo "release: created release $RELEASE_ID"
curl -fsS -X POST "$API/releases/$RELEASE_ID/assets?name=jtube-tv-$TAG.apk" \
-H "Authorization: token $FORGEJO_TOKEN" \
-F "attachment=@$APK" > /dev/null || fail "uploading the APK failed"
echo "release: published $SERVER/$REPO/releases/tag/$TAG"
echo "release: the app's Update row appears for anyone on an older versionCode."
echo "release: install it once by hand to confirm the build (docs/releases.md §5)."