#!/usr/bin/env bash set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" root="${DOBOTAI_SOURCE_ROOT:-$(cd "$script_dir/.." && pwd)}" go_bin="${GO_BIN:-/home/zheromiku/.local/share/go1.26.5/bin/go}" bin_dir="$root/bin" current="$bin_dir/dobotai" next="$bin_dir/dobotai.new" previous="$bin_dir/dobotai.prev" service="dobotai.service" version_url="http://127.0.0.1:8090/version" if [[ -n "$(git -C "$root" status --porcelain)" ]]; then echo "DobotAI source worktree is dirty; refusing unverifiable build" >&2 exit 1 fi commit_sha="$(git -C "$root" rev-parse HEAD)" version="${DOBOTAI_VERSION:-dev}" build_time="$(date -u '+%Y-%m-%dT%H:%M:%SZ')" ldflags="-X main.version=$version -X main.commitSHA=$commit_sha -X main.buildTime=$build_time" mkdir -p "$bin_dir" cd "$root" "$go_bin" build -trimpath -ldflags "$ldflags" -o "$next" ./cmd/dobotai chmod 0755 "$next" if [[ -f "$current" ]]; then cp -p "$current" "$previous" fi mv -f "$next" "$current" systemctl --user restart "$service" for _ in $(seq 1 30); do if curl --noproxy '*' --fail --silent --max-time 2 http://127.0.0.1:8090/healthz >/dev/null; then deployed_sha="$(curl --noproxy '*' --fail --silent --max-time 2 "$version_url" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("commit_sha", ""))' 2>/dev/null || true)" if [[ "$deployed_sha" == "$commit_sha" ]]; then echo "DOBOTAI_DEPLOY_OK" echo "DOBOTAI_COMMIT_SHA=$commit_sha" exit 0 fi fi sleep 1 done echo "DobotAI health check failed; attempting binary rollback" >&2 if [[ -f "$previous" ]]; then cp -p "$previous" "$current" systemctl --user restart "$service" fi systemctl --user status "$service" --no-pager >&2 || true exit 1