chore: 注入构建时版本信息到 /health 端点
现状:controller/tts.go:232 硬编码 Version: "2.0.0 (v3 API)",
不会随代码变化而更新,/health 无法反映实际跑的代码。
修复:四文件改动,实现构建时 ldflags 注入。
1. 新建 version/version.go,声明两个包级变量:
Version (默认 "dev")
Commit (默认 "dev")
2. dto/health.go:HealthResponse 加 Commit 字段(JSON 输出多一字段)
3. controller/tts.go:HealthResponse 用 version.Version / version.Commit
替代硬编码字面量
4. Dockerfile:加 ARG VERSION=dev ARG COMMIT=dev,
go build 时通过 -ldflags 注入到 version 包的两个变量
效果:
- 本地 go build (不传 ldflags) → version=dev commit=dev
- 开发 build (git describe) → version=v0.2.0-5-g4abcd5 commit=g4abcd5
- release build (打 tag 后) → version=v0.2.1 commit=<对应 hash>
测试环境(develop 分支,无 tag)显示距离上次 release 几个 commit +
具体 hash;生产环境(main + tag)显示干净 semver。
This commit is contained in:
+9
-1
@@ -7,7 +7,15 @@ RUN go mod download
|
|||||||
|
|
||||||
COPY . .
|
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
|
FROM alpine:3.21
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/volcano-tts/tts-api/middleware"
|
"github.com/volcano-tts/tts-api/middleware"
|
||||||
"github.com/volcano-tts/tts-api/setting"
|
"github.com/volcano-tts/tts-api/setting"
|
||||||
"github.com/volcano-tts/tts-api/telemetry"
|
"github.com/volcano-tts/tts-api/telemetry"
|
||||||
|
"github.com/volcano-tts/tts-api/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -229,7 +230,8 @@ func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
resp := dto.HealthResponse{
|
resp := dto.HealthResponse{
|
||||||
Status: status,
|
Status: status,
|
||||||
Service: "ByteDance TTS to OpenAI API Adapter",
|
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()),
|
Uptime: fmt.Sprintf("%.0f seconds", time.Since(startTime).Seconds()),
|
||||||
StartTime: startTime.Format(time.RFC3339),
|
StartTime: startTime.Format(time.RFC3339),
|
||||||
Memory: collectMemorySnapshot(),
|
Memory: collectMemorySnapshot(),
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ type HealthResponse struct {
|
|||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Service string `json:"service"`
|
Service string `json:"service"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
|
Commit string `json:"commit"`
|
||||||
Uptime string `json:"uptime"`
|
Uptime string `json:"uptime"`
|
||||||
StartTime string `json:"start_time"`
|
StartTime string `json:"start_time"`
|
||||||
Memory map[string]interface{} `json:"memory"`
|
Memory map[string]interface{} `json:"memory"`
|
||||||
|
|||||||
@@ -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"
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user