fix: embed release version in system update status

This commit is contained in:
ShukeBta
2026-07-03 01:55:30 +08:00
parent 470a639091
commit 78de029ef3
4 changed files with 47 additions and 4 deletions
+2
View File
@@ -56,5 +56,7 @@ jobs:
sbom: false sbom: false
tags: ${{ steps.meta.outputs.tags }} tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }} labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ env.RELEASE_VERSION }}
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
+3 -2
View File
@@ -8,7 +8,7 @@
# #
# Build: # Build:
# docker buildx build --platform linux/amd64,linux/arm64 \ # docker buildx build --platform linux/amd64,linux/arm64 \
# -t mediastation-go:latest --push . # --build-arg VERSION=MediaStationGo-v0.1.16 -t mediastation-go:latest --push .
# #
# Optional Intel VAAPI/QSV runtime packages: # Optional Intel VAAPI/QSV runtime packages:
# docker buildx build --build-arg WITH_VAAPI=true ... # docker buildx build --build-arg WITH_VAAPI=true ...
@@ -29,6 +29,7 @@ FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS backend
ARG TARGETOS ARG TARGETOS
ARG TARGETARCH ARG TARGETARCH
ARG GOPROXY=https://proxy.golang.org,direct ARG GOPROXY=https://proxy.golang.org,direct
ARG VERSION=dev
ENV GOPROXY=${GOPROXY} ENV GOPROXY=${GOPROXY}
WORKDIR /app WORKDIR /app
COPY go.mod go.sum ./ COPY go.mod go.sum ./
@@ -38,7 +39,7 @@ COPY . .
COPY --from=frontend /app/web/dist ./web/dist COPY --from=frontend /app/web/dist ./web/dist
RUN --mount=type=cache,target=/go/pkg/mod \ RUN --mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -trimpath -ldflags="-s -w" -o mediastation-go ./cmd/server go build -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o mediastation-go ./cmd/server
# ---- Stage 3: runtime ------------------------------------------------------ # ---- Stage 3: runtime ------------------------------------------------------
FROM alpine:3.23 FROM alpine:3.23
+18 -2
View File
@@ -18,6 +18,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@@ -33,6 +34,20 @@ import (
// version is overwritten at build time via -ldflags="-X main.version=...". // version is overwritten at build time via -ldflags="-X main.version=...".
var version = "dev" var version = "dev"
func effectiveVersion(buildVersion string) string {
buildVersion = strings.TrimSpace(buildVersion)
if buildVersion != "" && buildVersion != "dev" {
return buildVersion
}
if envVersion := strings.TrimSpace(os.Getenv("MEDIASTATION_VERSION")); envVersion != "" {
return envVersion
}
if buildVersion == "" {
return "dev"
}
return buildVersion
}
func main() { func main() {
cfg, err := config.Load() cfg, err := config.Load()
if err != nil { if err != nil {
@@ -47,8 +62,9 @@ func main() {
} }
defer func() { _ = logger.Sync() }() defer func() { _ = logger.Sync() }()
appVersion := effectiveVersion(version)
logger.Info("starting MediaStationGo", logger.Info("starting MediaStationGo",
zap.String("version", version), zap.String("version", appVersion),
zap.Int("port", cfg.App.Port), zap.Int("port", cfg.App.Port),
zap.String("data_dir", cfg.App.DataDir), zap.String("data_dir", cfg.App.DataDir),
) )
@@ -77,7 +93,7 @@ func main() {
repos := repository.New(db) repos := repository.New(db)
service.ApplyRuntimeSettings(context.Background(), cfg, repos, logger) service.ApplyRuntimeSettings(context.Background(), cfg, repos, logger)
applyCPUThreadLimit(cfg, logger) applyCPUThreadLimit(cfg, logger)
services := service.NewWithVersion(cfg, logger, repos, version) services := service.NewWithVersion(cfg, logger, repos, appVersion)
if repaired, err := services.RepairCloudPathMetadata(context.Background()); err != nil { if repaired, err := services.RepairCloudPathMetadata(context.Background()); err != nil {
logger.Warn("cloud path metadata repair failed", zap.Error(err)) logger.Warn("cloud path metadata repair failed", zap.Error(err))
+24
View File
@@ -11,6 +11,30 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func TestEffectiveVersionPrefersBuildVersion(t *testing.T) {
t.Setenv("MEDIASTATION_VERSION", "MediaStationGo-v0.1.15")
if got := effectiveVersion("MediaStationGo-v0.1.16"); got != "MediaStationGo-v0.1.16" {
t.Fatalf("effectiveVersion = %q, want MediaStationGo-v0.1.16", got)
}
}
func TestEffectiveVersionUsesEnvWhenBuildVersionIsDev(t *testing.T) {
t.Setenv("MEDIASTATION_VERSION", " MediaStationGo-v0.1.16 ")
if got := effectiveVersion("dev"); got != "MediaStationGo-v0.1.16" {
t.Fatalf("effectiveVersion = %q, want MediaStationGo-v0.1.16", got)
}
}
func TestEffectiveVersionDefaultsToDev(t *testing.T) {
t.Setenv("MEDIASTATION_VERSION", "")
if got := effectiveVersion(""); got != "dev" {
t.Fatalf("effectiveVersion = %q, want dev", got)
}
}
func TestServeSPANoCachesIndexAndServesRoutes(t *testing.T) { func TestServeSPANoCachesIndexAndServesRoutes(t *testing.T) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
webDir := t.TempDir() webDir := t.TempDir()