onionwire/.forgejo/workflows/android-release.yml

456 lines
20 KiB
YAML
Raw Permalink Normal View History

name: android-release
# Build the Android APK + SDK AAR on docker-amd64 and attach them to the
# Forgejo release for the computed (or requested) tag.
#
# The runner job image is node:20-bullseye (EOL). Do not apt-get there.
# toolchain runs in a sibling debian:bookworm container,
# the same stdin-tar + docker cp pattern as .forgejo/workflows/ci.yml.
#
# Publish is a separate job on the Pi runner (`docker`). POST /releases from
# the amd64 dind egress returns 422 with an empty body (core run 116);
# core's publish job on the Pi (run 135) is the proven path.
#
# First automated version with zero tags: v0.1.0. Afterwards, patch+1 from the
# latest v*.*.* tag UNLESS that tag's release is missing the Android assets —
# then join it (core owns tag creation). Never move or delete a tag.
#
# No automatic trigger, on purpose. Two candidate decoupled triggers were
# tried and both dead-end on Forgejo 10.0.3 (see docs/RELEASING.md):
# - `push: tags: ['v*.*.*']` — a tag created by release.yml (API or git push)
# does not fire it: pushes made with the CI's token are Actions-user events
# and Forgejo deliberately does not dispatch those back into Actions.
# Verified: the v0.2.0 and v0.2.1 tags produced no run, while hand-pushed
# tags did historically.
# - `workflow_call` from release.yml — reusable workflows only exist from
# Forgejo 12; this instance is 10.0.3.
# So a release is two dispatches: release.yml, then this one with `tag=<the
# tag the core just created>`. The core's run leaves the tag; this attaches
# the APK + AAR to it.
on:
workflow_dispatch:
inputs:
tag:
description: Existing tag to (re)build and attach (empty = next version)
required: false
type: string
dry_run:
description: Build and checksum without creating a tag or publishing (true/false)
required: false
type: string
default: "false"
workflow_call:
inputs:
tag:
required: false
type: string
dry_run:
required: false
type: string
default: "false"
concurrency:
group: onionwire-android-release-v2
cancel-in-progress: false
env:
CARGO_TERM_COLOR: never
CARGO_BUILD_JOBS: "1"
ONIONWIRE_ANDROID_ABI: arm64-v8a
ONIONWIRE_REQUIRE_RELEASE_KEY: "true"
ANDROID_IMAGE: debian:bookworm
ANDROID_CMDLINE_TOOLS_URL: https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
RUST_TOOLCHAIN: "1.91"
DOCKER_STATIC_URL: https://download.docker.com/linux/static/stable/x86_64/docker-27.5.1.tgz
BUILD_CONTAINER: onionwire-android-${{ github.run_id }}
SDK_VOLUME: onionwire-android-sdk
CARGO_VOLUME: onionwire-android-cargo-registry
TARGET_VOLUME: onionwire-android-target
GRADLE_VOLUME: onionwire-android-gradle
jobs:
android:
runs-on: docker-amd64
timeout-minutes: 90
outputs:
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.version.outputs.tag }}
version_code: ${{ steps.version.outputs.version_code }}
dry_run: ${{ steps.version.outputs.dry_run }}
steps:
- name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.inputs.tag || inputs.tag || github.sha }}
- name: Record runner resources
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
echo "workspace=$GITHUB_WORKSPACE hostname=$(hostname) arch=$(uname -m)"
nproc || true
free -h || true
df -h || true
ls -l /var/run/docker.sock || true
- name: Compute version
id: version
env:
INPUT_TAG: ${{ github.event.inputs.tag || inputs.tag }}
INPUT_DRY_RUN: ${{ github.event.inputs.dry_run }}
CALL_DRY_RUN: ${{ inputs.dry_run }}
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
REPO_API: ${{ github.server_url }}/api/v1/repos/${{ github.repository }}
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
echo "INPUT_DRY_RUN='${INPUT_DRY_RUN:-}' CALL_DRY_RUN='${CALL_DRY_RUN:-}' EVENT=${GITHUB_EVENT_NAME:-}"
git fetch --tags --force origin || true
input_tag="${INPUT_TAG:-}"
dry_run=false
case "${INPUT_DRY_RUN:-}" in true|TRUE|True|1|yes|YES) dry_run=true ;; esac
case "${CALL_DRY_RUN:-}" in true|TRUE|True|1|yes|YES) dry_run=true ;; esac
if [ -n "$input_tag" ]; then
ver="${input_tag#v}"
else
latest="$(git tag -l 'v*.*.*' --sort=-v:refname | head -n1 || true)"
if [ -z "$latest" ]; then
ver="0.1.0"
else
# Core owns tag creation. If the latest tag's release is missing
# the Android APK, join that version instead of minting patch+1.
join=0
if [ -n "${FORGEJO_TOKEN:-}" ]; then
http=$(curl -sS --http1.1 -o /tmp/latest-rel.json -w '%{http_code}' \
-u "sirius:${FORGEJO_TOKEN}" \
"$REPO_API/releases/tags/$latest" || true)
if [ "$http" != "200" ]; then
join=1
echo "latest tag $latest has no release (http $http); joining"
elif node -e 'const d=require("/tmp/latest-rel.json"); const n=(d.assets||[]).some(a=>/onionwire-.*-android-.*\\.apk$/.test(a.name)); process.exit(n?0:1)'; then
echo "latest tag $latest already has an Android APK; patch+1"
else
join=1
echo "latest tag $latest release is missing Android APK; joining"
fi
else
echo "FORGEJO_TOKEN unset; cannot inspect release assets, joining $latest"
join=1
fi
if [ "$join" = "1" ]; then
ver="${latest#v}"
else
ver="${latest#v}"
major="${ver%%.*}"
rest="${ver#*.}"
minor="${rest%%.*}"
patch="${rest#*.}"
patch=$((patch + 1))
ver="${major}.${minor}.${patch}"
fi
fi
fi
echo "$ver" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' || {
echo "not a MAJOR.MINOR.PATCH version: $ver" >&2
exit 1
}
major="${ver%%.*}"
rest="${ver#*.}"
minor="${rest%%.*}"
patch="${rest#*.}"
code=$((major * 10000 + minor * 100 + patch))
tag="v$ver"
{
echo "version=$ver"
echo "tag=$tag"
echo "version_code=$code"
echo "dry_run=$dry_run"
} >> "$GITHUB_OUTPUT"
echo "version=$ver tag=$tag versionCode=$code dry_run=$dry_run"
- name: Decode release keystore
env:
ANDROID_KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
: "${ANDROID_KEYSTORE_BASE64:?ANDROID_KEYSTORE_BASE64 secret is missing}"
: "${ANDROID_KEYSTORE_PASSWORD:?ANDROID_KEYSTORE_PASSWORD secret is missing}"
: "${ANDROID_KEY_ALIAS:?ANDROID_KEY_ALIAS secret is missing}"
: "${ANDROID_KEY_PASSWORD:?ANDROID_KEY_PASSWORD secret is missing}"
mkdir -p "$HOME/.android"
chmod 700 "$HOME/.android"
ks="$HOME/.android/onionwire-release.jks"
envf="$HOME/.android/onionwire-release.env"
printf '%s' "$ANDROID_KEYSTORE_BASE64" | base64 -d > "$ks"
chmod 600 "$ks"
test -s "$ks"
umask 077
printf 'ANDROID_KEYSTORE_FILE=%s\nANDROID_KEYSTORE_PASSWORD=%s\nANDROID_KEY_ALIAS=%s\nANDROID_KEY_PASSWORD=%s\n' \
"/root/.android/onionwire-release.jks" "$ANDROID_KEYSTORE_PASSWORD" "$ANDROID_KEY_ALIAS" "$ANDROID_KEY_PASSWORD" > "$envf"
chmod 600 "$envf"
echo "keystore bytes=$(wc -c < "$ks") staged for the build container"
- name: Ensure docker CLI
run: |
set -euo pipefail
if ! command -v docker >/dev/null 2>&1; then
echo "docker CLI missing in job image; fetching static client"
curl -fsSL "$DOCKER_STATIC_URL" -o /tmp/docker.tgz
tar -xzf /tmp/docker.tgz -C /tmp
install -m 755 /tmp/docker/docker /usr/local/bin/docker
fi
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
docker version
docker info >/tmp/docker-info.txt
grep -E 'Server Version|Operating System|Total Memory|Name' /tmp/docker-info.txt || true
- name: Build APK + AAR in bookworm container
id: build
env:
VERSION: ${{ steps.version.outputs.version }}
VERSION_CODE: ${{ steps.version.outputs.version_code }}
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
start=$(date +%s)
cat > .ci-android-build.sh <<'INNER'
#!/usr/bin/env bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y --no-install-recommends \
openjdk-17-jdk-headless python3 unzip curl ca-certificates file build-essential pkg-config git
mkdir -p /root/.android
cp /src/.ci-android/onionwire-release.jks /root/.android/onionwire-release.jks
cp /src/.ci-android/onionwire-release.env /root/.android/onionwire-release.env
chmod 600 /root/.android/onionwire-release.jks /root/.android/onionwire-release.env
# shellcheck disable=SC1091
set -a
source /root/.android/onionwire-release.env
set +a
if [ ! -x /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager ]; then
mkdir -p /opt/android-sdk/cmdline-tools
curl -fsSL "$ANDROID_CMDLINE_TOOLS_URL" -o /tmp/cmdline-tools.zip
rm -rf /tmp/cmdline-tools
unzip -q /tmp/cmdline-tools.zip -d /tmp/cmdline-tools
mkdir -p /opt/android-sdk/cmdline-tools/latest
cp -a /tmp/cmdline-tools/cmdline-tools/. /opt/android-sdk/cmdline-tools/latest/
fi
yes | /opt/android-sdk/cmdline-tools/latest/bin/sdkmanager --sdk_root=/opt/android-sdk --licenses >/tmp/sdk-licenses.log || true
/opt/android-sdk/cmdline-tools/latest/bin/sdkmanager --sdk_root=/opt/android-sdk \
"platform-tools" "platforms;android-36" "build-tools;36.0.0" "ndk;28.2.13676358"
test -d /opt/android-sdk/ndk/28.2.13676358
export ANDROID_HOME=/opt/android-sdk ANDROID_SDK_ROOT=/opt/android-sdk
export ANDROID_NDK_HOME=/opt/android-sdk/ndk/28.2.13676358
export PATH="$ANDROID_HOME/build-tools/36.0.0:$ANDROID_HOME/platform-tools:$PATH"
if [ ! -x /cargo/bin/rustup ]; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain "$RUST_TOOLCHAIN" --no-modify-path --profile minimal
fi
export PATH="/cargo/bin:$PATH"
rustup toolchain install "$RUST_TOOLCHAIN"
rustup default "$RUST_TOOLCHAIN"
rustup target add aarch64-linux-android
command -v cargo-ndk >/dev/null || cargo install cargo-ndk --locked
echo "=== toolchain ==="
java -version
rustc --version
cargo ndk --version
du -sh /opt/android-sdk /cargo /cargo-target /gradle || true
scripts/build-android-local.sh "$ONIONWIRE_VERSION"
version="$ONIONWIRE_VERSION"
abi="${ONIONWIRE_ANDROID_ABI:-arm64-v8a}"
apk="dist/onionwire-$version-android-$abi.apk"
aar="dist/onionwire-sdk-$version.aar"
{
echo "=== file ==="
file "$apk" "$aar"
echo "=== unzip lib ==="
unzip -l "$apk" | grep -E 'lib/.+\.so|AndroidManifest.xml' || true
unzip -l "$apk" | grep "lib/$abi/libonionwire_sdk.so"
echo "=== sha256 ==="
( cd dist && sha256sum -c "onionwire-$version-android-$abi.apk.sha256" "onionwire-sdk-$version.aar.sha256" )
echo "=== apksigner ==="
apksigner verify --print-certs "$apk"
echo "=== aapt ==="
aapt dump badging "$apk" | sed -n '1,20p' || true
echo "=== sizes ==="
wc -c "$apk" "$aar"
} | tee dist/ci-verify.log
INNER
chmod +x .ci-android-build.sh
docker volume create "$SDK_VOLUME" >/dev/null
docker volume create "$CARGO_VOLUME" >/dev/null
docker volume create "$TARGET_VOLUME" >/dev/null
docker volume create "$GRADLE_VOLUME" >/dev/null
docker rm -f "$BUILD_CONTAINER" >/dev/null 2>&1 || true
docker pull "$ANDROID_IMAGE"
docker create --name "$BUILD_CONTAINER" -i \
-e CARGO_TERM_COLOR=never \
-e CARGO_BUILD_JOBS="$CARGO_BUILD_JOBS" \
-e CARGO_HOME=/cargo \
-e CARGO_TARGET_DIR=/cargo-target \
-e GRADLE_USER_HOME=/gradle \
-e ANDROID_HOME=/opt/android-sdk \
-e ANDROID_SDK_ROOT=/opt/android-sdk \
-e ANDROID_NDK_HOME=/opt/android-sdk/ndk/28.2.13676358 \
-e ONIONWIRE_ANDROID_ABI="$ONIONWIRE_ANDROID_ABI" \
-e ONIONWIRE_REQUIRE_RELEASE_KEY=true \
-e ONIONWIRE_VERSION_CODE="$VERSION_CODE" \
-e ONIONWIRE_VERSION="$VERSION" \
-e RUST_TOOLCHAIN="$RUST_TOOLCHAIN" \
-e ANDROID_CMDLINE_TOOLS_URL="$ANDROID_CMDLINE_TOOLS_URL" \
-e GRADLE_OPTS="-Dorg.gradle.daemon=false -Dorg.gradle.jvmargs=-Xmx1024m -XX:MaxMetaspaceSize=384m -Dfile.encoding=UTF-8" \
-v "$SDK_VOLUME":/opt/android-sdk \
-v "$CARGO_VOLUME":/cargo \
-v "$TARGET_VOLUME":/cargo-target \
-v "$GRADLE_VOLUME":/gradle \
-w /src \
"$ANDROID_IMAGE" \
bash -euxc 'mkdir -p /src && tar xzf - -C /src && cd /src && bash /src/.ci-android-build.sh'
# Staging dir is included in the tar as .ci-android/ (gitignored paths).
mkdir -p .ci-android
cp "$HOME/.android/onionwire-release.jks" .ci-android/onionwire-release.jks
cp "$HOME/.android/onionwire-release.env" .ci-android/onionwire-release.env
tar czf - \
--exclude=./target --exclude=./.git --exclude=./.worktrees \
--exclude=./android/.gradle --exclude=./android/app/build \
--exclude=./android/sdk/build --exclude=./dist \
--exclude=./crates/onionwire-sdk/target \
. \
| docker start -a -i "$BUILD_CONTAINER"
mkdir -p dist
docker cp "$BUILD_CONTAINER:/src/dist/." dist/
end=$(date +%s)
echo "build_wall_clock_seconds=$((end - start))"
echo "wall_clock_seconds=$((end - start))" >> "$GITHUB_OUTPUT"
docker rm -f "$BUILD_CONTAINER" >/dev/null || true
rm -rf .ci-android
ls -l dist/
- name: Verify artifacts
run: |
set -euo pipefail
cd "${GITHUB_WORKSPACE}"
version="${{ steps.version.outputs.version }}"
abi="$ONIONWIRE_ANDROID_ABI"
apk="dist/onionwire-$version-android-$abi.apk"
aar="dist/onionwire-sdk-$version.aar"
[ -s "$apk" ] || { echo "missing or empty $apk" >&2; exit 1; }
[ -s "$aar" ] || { echo "missing or empty $aar" >&2; exit 1; }
apk_bytes=$(wc -c < "$apk")
aar_bytes=$(wc -c < "$aar")
echo "apk_bytes=$apk_bytes aar_bytes=$aar_bytes"
[ "$apk_bytes" -gt 0 ]
[ "$aar_bytes" -gt 0 ]
( cd dist && sha256sum -c "onionwire-$version-android-$abi.apk.sha256" "onionwire-sdk-$version.aar.sha256" )
echo "=== ci-verify.log (from bookworm build container) ==="
cat dist/ci-verify.log
echo "=== resources after build ==="
free -h || true
df -h || true
export DOCKER_HOST="${DOCKER_HOST:-unix:///var/run/docker.sock}"
docker images "$ANDROID_IMAGE" --format '{{.Repository}}:{{.Tag}} {{.Size}}' || true
docker system df || true
- name: Upload dist artifact
uses: https://code.forgejo.org/actions/upload-artifact@v3
with:
name: onionwire-android
path: dist/
if-no-files-found: error
retention-days: 1
- name: Scrub keystore
if: always()
run: |
set -euo pipefail
rm -rf "$HOME/.android/onionwire-release.jks" "$HOME/.android/onionwire-release.env" .ci-android
echo "keystore removed from runner"
publish:
needs: android
# Pi runner, not docker-amd64: POST /releases from amd64 dind returns 422
# with an empty body (core run 116). This job only curls the Forgejo API.
if: needs.android.result == 'success' && needs.android.outputs.dry_run != 'true'
runs-on: docker
timeout-minutes: 20
steps:
- name: Checkout CI tooling
uses: https://code.forgejo.org/actions/checkout@v4
with:
path: .ci-tools
- name: Download Android dist
uses: https://code.forgejo.org/actions/download-artifact@v3
with:
path: artifacts
- name: Publish (create-or-join)
env:
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
REPO_API: ${{ github.server_url }}/api/v1/repos/${{ github.repository }}
EVENT_SHA: ${{ github.sha }}
TAG: ${{ needs.android.outputs.tag }}
VERSION: ${{ needs.android.outputs.version }}
run: |
set -euo pipefail
: "${FORGEJO_TOKEN:?FORGEJO_TOKEN secret is missing}"
echo "publish host=$(hostname) arch=$(uname -m) tag=$TAG version=$VERSION"
mkdir -p dist
abi="$ONIONWIRE_ANDROID_ABI"
apk="onionwire-$VERSION-android-$abi.apk"
aar="onionwire-sdk-$VERSION.aar"
for f in "$apk" "$apk.sha256" "$aar" "$aar.sha256"; do
src=$(find artifacts -type f -name "$f" -print -quit)
if [ -z "$src" ]; then
echo "missing artifact $f" >&2
find artifacts -type f >&2 || true
exit 1
fi
cp "$src" "dist/$f"
done
for f in dist/"$apk" dist/"$apk".sha256 dist/"$aar" dist/"$aar".sha256; do
bytes=$(wc -c < "$f")
echo "on disk: $f $bytes bytes"
[ "$bytes" -gt 0 ] || { echo "refusing to publish empty $f" >&2; exit 1; }
file "$f" || true
done
tag_exists=0
i=0
while [ "$i" -lt 6 ]; do
code=$(curl -sS --http1.1 -o /tmp/tag.json -w '%{http_code}' \
-u "sirius:${FORGEJO_TOKEN}" \
"$REPO_API/tags/$TAG" || true)
if [ "$code" = "200" ]; then
tag_exists=1
echo "tag $TAG already exists; joining"
break
fi
i=$((i + 1))
echo "tag $TAG not found yet (http $code), wait 10s ($i/6)"
sleep 10
done
if [ "$tag_exists" = "1" ]; then
unset TARGET_COMMITISH || true
else
export TARGET_COMMITISH="$EVENT_SHA"
echo "creating tag $TAG at $TARGET_COMMITISH"
fi
echo "publishing $TAG from $REPO_API"
.ci-tools/scripts/publish-release.sh \
"$TAG" "OnionWire $TAG" \
.ci-tools/scripts/release-body.md \
"dist/$apk" \
"dist/$apk.sha256" \
"dist/$aar" \
"dist/$aar.sha256"