#!/bin/bash set -euo pipefail CONFIG_FILE="${RUSTPROXY_BUILDER_CONFIG:-/etc/rustproxy-opnsense-builder.env}" if [[ ! -r "$CONFIG_FILE" ]]; then echo "Missing builder config: $CONFIG_FILE" >&2 exit 1 fi # shellcheck disable=SC1090 source "$CONFIG_FILE" : "${PACKAGING_REPO_URL:?PACKAGING_REPO_URL is required}" : "${PACKAGING_REPO_REF:=HEAD}" : "${PACKAGING_WORKDIR:=/opt/opn-rustproxy/packaging}" : "${APP_REPO_URL:=https://git.linuxiarz.pl/gru/RustProxy}" : "${APP_REPO_REF:=HEAD}" : "${APP_WORKDIR:=/opt/opn-rustproxy/source/RustProxy}" : "${STATE_DIR:=/var/lib/rustproxy-opnsense-builder}" mkdir -p "$STATE_DIR" "$(dirname "$PACKAGING_WORKDIR")" "$(dirname "$APP_WORKDIR")" exec 9>"$STATE_DIR/build.lock" if ! flock -n 9; then echo "Another build is running; exiting." exit 0 fi SYNC_COMMIT="" sync_checkout() { local dir="$1" local url="$2" local ref="$3" if [[ ! -d "$dir/.git" ]]; then rm -rf "$dir" mkdir -p "$dir" git -C "$dir" init git -C "$dir" remote add origin "$url" else git -C "$dir" remote set-url origin "$url" fi echo "Fetching $url ($ref)" >&2 git -C "$dir" fetch --prune --tags origin "$ref" >&2 SYNC_COMMIT="$(git -C "$dir" rev-parse FETCH_HEAD)" git -C "$dir" checkout --detach -f "$SYNC_COMMIT" >&2 git -C "$dir" reset --hard "$SYNC_COMMIT" >&2 git -C "$dir" clean -ffd >&2 } # Refresh the packaging repository first so the newest build logic is used. sync_checkout "$PACKAGING_WORKDIR" "$PACKAGING_REPO_URL" "$PACKAGING_REPO_REF" packaging_commit="$SYNC_COMMIT" # RustProxy is intentionally NOT vendored in the packaging repository. # Fetch the application directly from its upstream repository every run. sync_checkout "$APP_WORKDIR" "$APP_REPO_URL" "$APP_REPO_REF" app_commit="$SYNC_COMMIT" app_revision="$(git -C "$APP_WORKDIR" rev-list --count "$app_commit")" if [[ ! -f "$APP_WORKDIR/Cargo.toml" ]]; then echo "Upstream checkout does not contain Cargo.toml: $APP_WORKDIR" >&2 exit 1 fi state_file="$STATE_DIR/last-successful-build" current_state="packaging=$packaging_commit app=$app_commit" last_state="$(cat "$state_file" 2>/dev/null || true)" if [[ "$current_state" == "$last_state" ]]; then echo "No changes. packaging=$packaging_commit app=$app_commit" exit 0 fi echo "Building RustProxy commit $app_commit (revision $app_revision)" echo "Using packaging commit $packaging_commit" RUSTPROXY_SOURCE_DIR="$APP_WORKDIR" \ RUSTPROXY_SOURCE_URL="$APP_REPO_URL" \ RUSTPROXY_SOURCE_REF="$APP_REPO_REF" \ RUSTPROXY_SOURCE_COMMIT="$app_commit" \ RUSTPROXY_SOURCE_REVISION="$app_revision" \ "$PACKAGING_WORKDIR/ci/build-release.sh" printf '%s\n' "$current_state" > "$state_file" echo "Build completed: $current_state"