feat: expose runtime build version

This commit is contained in:
姚章浩
2026-07-27 12:10:17 +08:00
parent aa0513eb59
commit 2c0ed7cc67
7 changed files with 159 additions and 5 deletions
+20 -4
View File
@@ -1,18 +1,30 @@
#!/usr/bin/env bash
set -euo pipefail
root="/home/zheromiku/dobotai-platform"
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 -o "$next" ./cmd/dobotai
"$go_bin" build -trimpath -ldflags "$ldflags" -o "$next" ./cmd/dobotai
chmod 0755 "$next"
if [[ -f "$current" ]]; then
@@ -24,8 +36,12 @@ 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
echo "DOBOTAI_DEPLOY_OK"
exit 0
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
+32
View File
@@ -0,0 +1,32 @@
package scripts
import (
"os"
"strings"
"testing"
)
func TestDeployLocalBuildsItsSelectedSourceAndVerifiesVersion(t *testing.T) {
data, err := os.ReadFile("deploy-local.sh")
if err != nil {
t.Fatal(err)
}
source := string(data)
if strings.Contains(source, `root="/home/zheromiku/dobotai-platform"`) {
t.Fatal("deploy script must not hard-code the production checkout")
}
for _, fragment := range []string{
`BASH_SOURCE[0]`,
`DOBOTAI_SOURCE_ROOT`,
`git -C "$root" status --porcelain`,
`git -C "$root" rev-parse HEAD`,
`main.version`,
`main.commitSHA`,
`main.buildTime`,
`http://127.0.0.1:8090/version`,
} {
if !strings.Contains(source, fragment) {
t.Errorf("deploy script missing %q", fragment)
}
}
}