diff --git a/Dockerfile b/Dockerfile index 101b7c5..95f50e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,15 @@ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -o tts-api . +# VERSION 由 CI/CD 传入,通常为 `git describe --tags --always --dirty` 的输出 +# COMMIT 为 `git rev-parse --short HEAD` +# 本地默认 dev +ARG VERSION=dev +ARG COMMIT=dev +RUN CGO_ENABLED=0 GOOS=linux go build \ + -ldflags "-X github.com/volcano-tts/tts-api/version.Version=${VERSION} \ + -X github.com/volcano-tts/tts-api/version.Commit=${COMMIT}" \ + -o tts-api . FROM alpine:3.21 diff --git a/controller/tts.go b/controller/tts.go index 5ed6495..bfff026 100644 --- a/controller/tts.go +++ b/controller/tts.go @@ -18,6 +18,7 @@ import ( "github.com/volcano-tts/tts-api/middleware" "github.com/volcano-tts/tts-api/setting" "github.com/volcano-tts/tts-api/telemetry" + "github.com/volcano-tts/tts-api/version" ) var ( @@ -229,7 +230,8 @@ func HealthHandler(w http.ResponseWriter, r *http.Request) { resp := dto.HealthResponse{ Status: status, Service: "ByteDance TTS to OpenAI API Adapter", - Version: "2.0.0 (v3 API)", + Version: version.Version, + Commit: version.Commit, Uptime: fmt.Sprintf("%.0f seconds", time.Since(startTime).Seconds()), StartTime: startTime.Format(time.RFC3339), Memory: collectMemorySnapshot(), diff --git a/dto/health.go b/dto/health.go index 5d844d9..9b9e83f 100644 --- a/dto/health.go +++ b/dto/health.go @@ -7,6 +7,7 @@ type HealthResponse struct { Status string `json:"status"` Service string `json:"service"` Version string `json:"version"` + Commit string `json:"commit"` Uptime string `json:"uptime"` StartTime string `json:"start_time"` Memory map[string]interface{} `json:"memory"` diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..74e3004 --- /dev/null +++ b/version/version.go @@ -0,0 +1,18 @@ +// Package version 提供构建时注入的版本信息。 +// +// Version 和 Commit 在编译时通过 -ldflags 注入: +// +// go build -ldflags "-X github.com/volcano-tts/tts-api/version.Version=$VERSION \ +// -X github.com/volcano-tts/tts-api/version.Commit=$COMMIT" +// +// 开发时默认 "dev",CI/CD 时通常由 git describe 自动算出: +// VERSION=$(git describe --tags --always --dirty) +// COMMIT=$(git rev-parse --short HEAD) +// +// /health 端点会暴露这两个值,方便运维确认"跑的到底是哪个 commit"。 +package version + +var ( + Version = "dev" + Commit = "dev" +)