From c00c46e7a18c173d49fb4ffff2a90d5354353e54 Mon Sep 17 00:00:00 2001 From: "3371392206@qq.com" <3371392206@qq.com> Date: Thu, 27 Aug 2026 10:18:34 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=B3=A8=E5=85=A5=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E6=97=B6=E7=89=88=E6=9C=AC=E4=BF=A1=E6=81=AF=E5=88=B0=20/healt?= =?UTF-8?q?h=20=E7=AB=AF=E7=82=B9=20=E7=8E=B0=E7=8A=B6:controller/tts.go:2?= =?UTF-8?q?32=20=E7=A1=AC=E7=BC=96=E7=A0=81=20Version:=20"2.0.0=20(v3=20AP?= =?UTF-8?q?I)",=20=E4=B8=8D=E4=BC=9A=E9=9A=8F=E4=BB=A3=E7=A0=81=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E8=80=8C=E6=9B=B4=E6=96=B0,/health=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=8F=8D=E6=98=A0=E5=AE=9E=E9=99=85=E8=B7=91=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E3=80=82=20=E4=BF=AE=E5=A4=8D:=E5=9B=9B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=B9=E5=8A=A8,=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=97=B6=20ldflags=20=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=E3=80=82=20=20=201.=20=E6=96=B0=E5=BB=BA=20version/version.go,?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E4=B8=A4=E4=B8=AA=E5=8C=85=E7=BA=A7=E5=8F=98?= =?UTF-8?q?=E9=87=8F:=20=20=20=20=20=20=20=20Version=20(=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=20"dev")=20=20=20=20=20=20=20=20Commit=20=20(=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=20"dev")=20=20=202.=20dto/health.go:HealthResponse=20?= =?UTF-8?q?=E5=8A=A0=20Commit=20=E5=AD=97=E6=AE=B5(JSON=20=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=A4=9A=E4=B8=80=E5=AD=97=E6=AE=B5)=20=20=203.=20con?= =?UTF-8?q?troller/tts.go:HealthResponse=20=E7=94=A8=20version.Version=20/?= =?UTF-8?q?=20version.Commit=20=20=20=20=20=20=E6=9B=BF=E4=BB=A3=E7=A1=AC?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=AD=97=E9=9D=A2=E9=87=8F=20=20=204.=20Dock?= =?UTF-8?q?erfile:=E5=8A=A0=20ARG=20VERSION=3Ddev=20ARG=20COMMIT=3Ddev,=20?= =?UTF-8?q?=20=20=20=20=20go=20build=20=E6=97=B6=E9=80=9A=E8=BF=87=20-ldfl?= =?UTF-8?q?ags=20=E6=B3=A8=E5=85=A5=E5=88=B0=20version=20=E5=8C=85?= =?UTF-8?q?=E7=9A=84=E4=B8=A4=E4=B8=AA=E5=8F=98=E9=87=8F=20=E6=95=88?= =?UTF-8?q?=E6=9E=9C:=20=20=20-=20=E6=9C=AC=E5=9C=B0=20go=20build=20(?= =?UTF-8?q?=E4=B8=8D=E4=BC=A0=20ldflags)=20=E2=86=92=20version=3Ddev=20com?= =?UTF-8?q?mit=3Ddev=20=20=20-=20=E5=BC=80=E5=8F=91=20build=20(git=20descr?= =?UTF-8?q?ibe)=20=E2=86=92=20version=3Dv0.2.0-5-g4abcd5=20commit=3Dg4abcd?= =?UTF-8?q?5=20=20=20-=20release=20build=20(=E6=89=93=20tag=20=E5=90=8E)?= =?UTF-8?q?=20=E2=86=92=20version=3Dv0.2.1=20commit=3D<=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=20hash>=20=E6=B5=8B=E8=AF=95=E7=8E=AF=E5=A2=83(develop=20?= =?UTF-8?q?=E5=88=86=E6=94=AF,=E6=97=A0=20tag)=E6=98=BE=E7=A4=BA=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB=E4=B8=8A=E6=AC=A1=20release=20=E5=87=A0=E4=B8=AA=20co?= =?UTF-8?q?mmit=20+=20=E5=85=B7=E4=BD=93=20hash;=E7=94=9F=E4=BA=A7?= =?UTF-8?q?=E7=8E=AF=E5=A2=83(main=20+=20tag)=E6=98=BE=E7=A4=BA=E5=B9=B2?= =?UTF-8?q?=E5=87=80=20semver=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 10 +++++++++- controller/tts.go | 4 +++- dto/health.go | 1 + version/version.go | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 version/version.go 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" +)