Files
VoelinClient/docker/android.Dockerfile
T
faumarayandClaude Sonnet 5.5 b89116c0f4 Let the Docker builds fetch the private VoelinProtocol repository with an ssh-agent
Every cargo command in the images (linux, windows, android and the gateway
image) failed for the same reason as the CI job: Cargo resolves the whole
workspace, which includes the git dependency voelin-identity on the private
Voelin/VoelinProtocol repository, and the images had no SSH access to it. The
crates that depend on voelin-identity are voelin-store and, through it,
voelin-core, voelin-ui, voelin-ui-vm, voelin-android and voelinctl; the
gateway crate does not, but `cargo build -p voelin-gateway` fails without
access too (checked with an empty CARGO_HOME), so its Dockerfile needs the
same.

Each cargo RUN gets --mount=type=ssh,required=true: the build is handed the
caller's ssh-agent (docker build --ssh default), the key stays in the agent
and never reaches a layer or the build context, and a build without --ssh
fails at that step with "unset ssh forward key default". The build stage
copies the host key committed in .gitea/known_hosts to
/etc/ssh/ssh_known_hosts and sets CARGO_NET_GIT_FETCH_WITH_CLI=true, so that
Cargo uses the system git and ssh. The toolchain stages install git and
openssh-client where they were missing; the gateway Dockerfile gets the
syntax directive the others already have. scripts/docker-build.sh passes
--ssh default and stops early, with a message, when ssh-add -l shows no agent
or no key.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 13:43:12 +03:00

117 lines
5.7 KiB
Docker

# syntax=docker/dockerfile:1
#
# Android APKs (docs/building.md, docs/android.md): a debug APK, installable
# as is, for arm64-v8a and x86_64 (emulators).
#
# docker build --ssh default -f docker/android.Dockerfile -o dist/android .
# docker build --ssh default -f docker/android.Dockerfile --build-arg ABIS=arm64-v8a -o dist/android .
#
# or scripts/docker-build.sh android. --ssh default hands the build your
# ssh-agent, which must hold a key that can read the private VoelinProtocol
# repository (docs/building.md, "Depending on VoelinProtocol"). A signed
# release APK needs your keystore (docs/release.md), passed as build secrets:
#
# docker build --ssh default -f docker/android.Dockerfile --build-arg APK=release \
# --secret id=keystore,src=release.jks --secret id=signing,src=signing.env \
# -o dist/android .
#
# signing.env holds VOELIN_SIGNING_STORE_PASSWORD, VOELIN_SIGNING_KEY_ALIAS and
# VOELIN_SIGNING_KEY_PASSWORD lines (NAME=value). Without them a release APK is
# unsigned.
FROM ubuntu:24.04 AS toolchain
ARG DEBIAN_FRONTEND=noninteractive
# JDK 17 (Gradle, the Android Gradle plugin), CMake + Ninja (the bundled libopus).
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential ca-certificates cmake curl git ninja-build openjdk-17-jdk-headless \
openssh-client pkg-config python3 unzip \
&& rm -rf /var/lib/apt/lists/*
# Extra CA certificates (PEM, one or more), for building behind a
# TLS-inspecting proxy; scripts/docker-build.sh passes $BUILD_CA_CERT as this
# secret. One file per certificate, so the Java truststore gets each one too.
RUN --mount=type=secret,id=ca,required=false \
if [ -s /run/secrets/ca ]; then \
awk '/-----BEGIN CERTIFICATE-----/ { n++ } n { print > ("/usr/local/share/ca-certificates/build-ca-" n ".crt") }' \
/run/secrets/ca \
&& update-ca-certificates; \
fi
# Java tools do not read HTTPS_PROXY: turn it into JVM properties for
# sdkmanager and Gradle when it is set (GRADLE_OPTS for the wrapper's JVM,
# -D arguments for the build JVM).
COPY <<-'EOF' /usr/local/bin/java-proxy-opts
#!/bin/sh
p=${HTTPS_PROXY:-${https_proxy:-}}
[ -n "$p" ] || exit 0
hp=${p#*://}; hp=${hp%%/*}; hp=${hp##*@}
echo "-Dhttps.proxyHost=${hp%:*} -Dhttps.proxyPort=${hp##*:} -Dhttp.proxyHost=${hp%:*} -Dhttp.proxyPort=${hp##*:}"
EOF
RUN chmod +x /usr/local/bin/java-proxy-opts
# The SDK packages the release workflow installs (.github/workflows/release.yml,
# package-android job).
ENV ANDROID_HOME=/opt/android-sdk
ARG CMDLINE_TOOLS=commandlinetools-linux-16111833_latest.zip
ARG CMDLINE_TOOLS_SHA1=e025545c62a8e64c7559119566a569fb1dec5f60
ARG NDK_VERSION=27.3.13750724
RUN curl -sSfL -o /tmp/tools.zip "https://dl.google.com/android/repository/$CMDLINE_TOOLS" \
&& echo "$CMDLINE_TOOLS_SHA1 /tmp/tools.zip" | sha1sum -c - \
&& mkdir -p "$ANDROID_HOME/cmdline-tools" \
&& unzip -q /tmp/tools.zip -d "$ANDROID_HOME/cmdline-tools" \
&& mv "$ANDROID_HOME/cmdline-tools/cmdline-tools" "$ANDROID_HOME/cmdline-tools/latest" \
&& rm /tmp/tools.zip \
&& export JAVA_OPTS="$(java-proxy-opts)" \
&& yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null \
&& "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \
"ndk;$NDK_VERSION" "platforms;android-36" "build-tools;36.0.0" >/dev/null
ENV ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VERSION ANDROID_NDK_ROOT=$ANDROID_HOME/ndk/$NDK_VERSION
ENV RUSTUP_HOME=/usr/local/rustup CARGO_HOME=/usr/local/cargo PATH=/usr/local/cargo/bin:$PATH
COPY rust-toolchain.toml /tmp/toolchain/
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
| sh -s -- -y --no-modify-path --profile minimal --default-toolchain none \
&& cd /tmp/toolchain && rustup toolchain install \
&& rustup target add --toolchain "$(sed -n 's/^channel = "\(.*\)"/\1/p' rust-toolchain.toml)" \
aarch64-linux-android x86_64-linux-android armv7-linux-androideabi \
&& cargo install --locked cargo-ndk --version 4.1.2 \
&& rm -rf "$CARGO_HOME/registry"
FROM toolchain AS build
# Comma-separated Android ABIs: arm64-v8a, x86_64, armeabi-v7a.
ARG ABIS=arm64-v8a,x86_64
# debug (installable as is) or release (signed with the build secrets).
ARG APK=debug
# BuildKit secrets do not invalidate cached RUN steps. A signed CI build
# supplies a fresh non-secret nonce so a rotated key cannot reuse an old APK.
ARG SIGNING_CACHE_BUST=unsigned
WORKDIR /src
# voelin-identity comes from the private VoelinProtocol repository, fetched
# over SSH by Cargo through the system git, with the agent that
# `docker build --ssh default` forwards (scripts/docker-build.sh passes it):
# the key stays in the agent and never reaches a layer. The host key is the
# reviewed line from .gitea/known_hosts. Cargo resolves the whole workspace, so
# every cargo command needs this (Gradle's cargo-ndk inherits the agent and the
# variable below).
COPY .gitea/known_hosts /etc/ssh/ssh_known_hosts
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
COPY . .
RUN --mount=type=ssh,required=true \
--mount=type=cache,id=voelin-cargo-registry,target=/usr/local/cargo/registry \
--mount=type=cache,id=voelin-target-android,target=/src/target \
--mount=type=cache,id=voelin-gradle,target=/root/.gradle \
--mount=type=secret,id=keystore,required=false \
--mount=type=secret,id=signing,required=false \
set -e; : "$SIGNING_CACHE_BUST"; \
if [ "$APK" = release ] && [ -f /run/secrets/keystore ]; then \
export VOELIN_SIGNING_STORE_FILE=/run/secrets/keystore; \
set -a; . /run/secrets/signing; set +a; \
fi; \
proxy="$(java-proxy-opts)"; \
export GRADLE_OPTS="$proxy"; \
task="assemble$(echo "$APK" | sed 's/./\U&/')"; \
(cd android && ./gradlew --no-daemon $proxy "$task" -Pvoelin.abis="$ABIS") \
&& scripts/package.sh android /out
FROM scratch AS artifacts
COPY --from=build /out /