#!/usr/bin/env sh # Build the release binaries here and deploy them to Fly in a runtime-only # image, so Fly's builder never compiles zds. ZDS_PREBUILT names an install # prefix that already holds them (CI's tools/ci-checks.sh writes .ci-release). # Extra arguments go to `flyctl deploy` (for example --build-only). set -eu cd "$(dirname "$0")/.." zig=${ZIG:-zig} app=zds-pds label="git-$(git rev-parse --short=12 HEAD 2>/dev/null || date +%s)" stage=$(mktemp -d) trap 'rm -rf "$stage"' EXIT if [ -n "${ZDS_PREBUILT:-}" ]; then cp -R "$ZDS_PREBUILT/bin" "$stage/bin" else "$zig" build deploy-build -Dtarget=x86_64-linux-gnu -Doptimize=ReleaseSafe -Dweb-garden=true \ -Dweb-drawings=src/internal/web/icons/generated.zig -p "$stage" fi cp Dockerfile.fly "$stage/Dockerfile" cp config/litestream.yml tools/container-start.sh "$stage/" cp fly.toml "$stage/fly.toml" # flyctl's own wait has hung for minutes after the machine was already # running the new image, so detach and check the swap here instead. The remote # builder's context upload has also stalled for 16 minutes before flyctl gave # up and retried (2026-11-22), so each attempt is cut off and retried here; a # normal attempt takes under a minute. attempt=1 until (cd "$stage" && timeout "${FLY_DEPLOY_ATTEMPT_TIMEOUT:-180}" flyctl deploy --remote-only --image-label "$label" --detach "$@"); do if [ "$attempt" -ge 3 ]; then echo "flyctl deploy failed after $attempt attempts" >&2 exit 1 fi echo "flyctl deploy failed or stalled (attempt $attempt); retrying" >&2 attempt=$((attempt + 1)) done case " $* " in *" --build-only "*) exit 0 ;; esac deadline=$(($(date +%s) + 180)) while :; do machines=$(flyctl machine list -a "$app" --json) stale=$(printf %s "$machines" | jq --arg label "$label" '[.[] | select(.image_ref.tag != $label or .state != "started")] | length') if [ "$stale" = 0 ] && curl -fsS -m 5 https://pds.zat.dev/xrpc/_health; then echo echo "deployed $label" exit 0 fi if [ "$(date +%s)" -ge "$deadline" ]; then printf %s "$machines" | jq -c '.[] | {id, state, tag: .image_ref.tag}' >&2 echo "machines did not reach $label within 180s" >&2 exit 1 fi sleep 3 done