82 lines
4.7 KiB
Bash
Executable File
82 lines
4.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Publishes the current Android build as a release on the Gitea/Forgejo server this repo is pushed to:
|
|
# tags v<version>, creates the release and attaches releases/pikkie-<version>.apk.
|
|
# (An F-Droid repo that watches the releases picks the APK up from there.)
|
|
#
|
|
# ./scripts/publish-release.sh # release the version in android/app/build.gradle.kts
|
|
# ./scripts/publish-release.sh --dry-run # show what would happen
|
|
#
|
|
# Needs an access token with "write:repository" scope, from Settings → Applications on the Gitea site.
|
|
# Provide it as GITEA_TOKEN=… or put it in ~/.pikkie/gitea-token (chmod 600). It is never printed.
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
|
|
DRY=0; [[ "${1:-}" == "--dry-run" ]] && DRY=1
|
|
VERSION=$(sed -n 's/.*versionName = "\(.*\)".*/\1/p' android/app/build.gradle.kts | head -1)
|
|
CODE=$(sed -n 's/.*versionCode = \([0-9]*\).*/\1/p' android/app/build.gradle.kts | head -1)
|
|
TAG="v$VERSION"
|
|
APK="releases/pikkie-$VERSION.apk"
|
|
[[ -f "$APK" ]] || { echo "✖ $APK not found. Run ./scripts/build-apk.sh first (with the release key)."; exit 1; }
|
|
[[ "$APK" != *-debug.apk ]] || { echo "✖ refusing to publish a debug build"; exit 1; }
|
|
|
|
# where to publish: derived from the git remote (override with GITEA_URL / GITEA_REPO)
|
|
REMOTE=$(git remote get-url origin)
|
|
HOST=$(echo "$REMOTE" | sed -E 's#^(ssh://)?([^@]+@)?([^:/]+).*#\3#; s#^https?://([^/]+).*#\1#')
|
|
REPO=${GITEA_REPO:-$(echo "$REMOTE" | sed -E 's#\.git$##; s#^.*[:/]([^/]+/[^/]+)$#\1#')}
|
|
API="${GITEA_URL:-https://$HOST}/api/v1"
|
|
|
|
SHA=$(shasum -a 256 "$APK" | cut -d' ' -f1)
|
|
CERT=""
|
|
SIGNER="${ANDROID_HOME:-/opt/homebrew/share/android-commandlinetools}/build-tools/35.0.0/apksigner"
|
|
if [[ -x "$SIGNER" ]]; then
|
|
[[ -z "${JAVA_HOME:-}" && -d /opt/homebrew/opt/openjdk@17 ]] && export JAVA_HOME=/opt/homebrew/opt/openjdk@17 PATH="/opt/homebrew/opt/openjdk@17/bin:$PATH"
|
|
CERT=$("$SIGNER" verify --print-certs "$APK" 2>/dev/null | sed -n 's/.*certificate SHA-256 digest: //p' | head -1)
|
|
fi
|
|
NOTES="PIKKIE for Android $VERSION (versionCode $CODE)
|
|
|
|
APK SHA-256: \`$SHA\`
|
|
Signing certificate SHA-256: \`${CERT:-unknown}\`
|
|
|
|
Install it from here, from your PIKKIE server's download page, or let the app update itself."
|
|
|
|
echo "release $TAG → $API/repos/$REPO"
|
|
echo "asset $APK ($(du -h "$APK" | cut -f1 | tr -d ' ')) sha256 ${SHA:0:16}…"
|
|
[[ -n "$CERT" ]] && echo "signed ${CERT:0:16}…"
|
|
|
|
if [[ -n "$(git status --porcelain)" ]]; then echo "✖ uncommitted changes. Commit first, so the tag points at what was built."; exit 1; fi
|
|
git fetch -q origin
|
|
[[ "$(git rev-parse HEAD)" == "$(git rev-parse '@{u}')" ]] || { echo "✖ local commits are not pushed yet. Push first."; exit 1; }
|
|
(( DRY )) && { echo "(dry run: nothing tagged or uploaded)"; exit 0; }
|
|
|
|
TOKEN="${GITEA_TOKEN:-}"
|
|
[[ -z "$TOKEN" && -f "$HOME/.pikkie/gitea-token" ]] && TOKEN=$(tr -d '[:space:]' < "$HOME/.pikkie/gitea-token")
|
|
[[ -n "$TOKEN" ]] || { echo "✖ no access token. Set GITEA_TOKEN or create ~/.pikkie/gitea-token (see the top of this script)."; exit 1; }
|
|
# the token goes to curl through a file descriptor, never on a command line (where other processes could see it)
|
|
api() { curl -sS -H @<(printf 'Authorization: token %s\n' "$TOKEN") "$@"; }
|
|
|
|
if ! git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then git tag -a "$TAG" -m "PIKKIE $VERSION"; fi
|
|
git push -q origin "$TAG"
|
|
|
|
REL=$(api "$API/repos/$REPO/releases/tags/$TAG")
|
|
ID=$(echo "$REL" | jq -r '.id // empty')
|
|
# Gitea registers a freshly pushed tag in the background; creating the release in the same instant can hit
|
|
# a duplicate-key error, so give it a few tries
|
|
for try in 1 2 3 4 5; do
|
|
[[ -n "$ID" ]] && break
|
|
REL=$(api -X POST "$API/repos/$REPO/releases" -H 'Content-Type: application/json' \
|
|
-d "$(jq -n --arg t "$TAG" --arg n "PIKKIE $VERSION" --arg b "$NOTES" '{tag_name:$t, name:$n, body:$b}')")
|
|
ID=$(echo "$REL" | jq -r '.id // empty')
|
|
[[ -n "$ID" ]] && break
|
|
(( try < 5 )) && { sleep 2; ID=$(api "$API/repos/$REPO/releases/tags/$TAG" | jq -r '.id // empty'); }
|
|
done
|
|
[[ -n "$ID" ]] || { echo "✖ could not create the release: $(echo "$REL" | jq -r '.message // .' | head -c 300)"; exit 1; }
|
|
NAME=$(basename "$APK")
|
|
# replace an asset of the same name (re-running the script for the same version)
|
|
OLD=$(echo "$REL" | jq -r --arg n "$NAME" '.assets[]? | select(.name==$n) | .id')
|
|
[[ -n "$OLD" ]] && api -X DELETE "$API/repos/$REPO/releases/$ID/assets/$OLD" >/dev/null
|
|
UP=$(api -X POST "$API/repos/$REPO/releases/$ID/assets?name=$NAME" -F "attachment=@$APK;type=application/vnd.android.package-archive")
|
|
URL=$(echo "$UP" | jq -r '.browser_download_url // empty')
|
|
[[ -n "$URL" ]] || { echo "✖ upload failed: $(echo "$UP" | jq -r '.message // .' | head -c 300)"; exit 1; }
|
|
echo "✔ published $TAG"
|
|
echo " $URL"
|