From c77c491b64d8c301284161ef98f0622fb3eb08c4 Mon Sep 17 00:00:00 2001 From: Ryan Hamamura <58859899+ryanhamamura@users.noreply.github.com> Date: Tue, 3 Mar 2026 09:23:37 -1000 Subject: [PATCH] feat: display app version in UI footer - Add version package with build-time variables - Inject version via ldflags in Dockerfile using git describe - Show version in footer on every page - Log version and commit on server startup --- Dockerfile | 6 ++++-- features/common/layouts/base.templ | 8 +++++++- main.go | 3 ++- version/version.go | 10 ++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 version/version.go diff --git a/Dockerfile b/Dockerfile index 0b62d6c..caac1b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM docker.io/golang:1.25.4-alpine AS build -RUN apk add --no-cache upx +RUN apk add --no-cache upx git WORKDIR /src COPY go.mod go.sum ./ @@ -10,7 +10,9 @@ COPY . . RUN go tool templ generate RUN go tool gotailwind -i assets/css/input.css -o assets/css/output.css --minify RUN --mount=type=cache,target=/root/.cache/go-build \ - CGO_ENABLED=0 go build -ldflags="-s" -o /bin/games . + VERSION=$(git describe --tags --always) && \ + COMMIT=$(git rev-parse --short HEAD) && \ + CGO_ENABLED=0 go build -ldflags="-s -X github.com/ryanhamamura/games/version.Version=$VERSION -X github.com/ryanhamamura/games/version.Commit=$COMMIT" -o /bin/games . RUN upx -9 -k /bin/games FROM scratch diff --git a/features/common/layouts/base.templ b/features/common/layouts/base.templ index 86d563a..5029e7f 100644 --- a/features/common/layouts/base.templ +++ b/features/common/layouts/base.templ @@ -1,6 +1,9 @@ package layouts -import "github.com/ryanhamamura/games/config" +import ( + "github.com/ryanhamamura/games/config" + "github.com/ryanhamamura/games/version" +) templ Base(title string) { @@ -16,6 +19,9 @@ templ Base(title string) {
} { children... } + } diff --git a/main.go b/main.go index 041e13c..9170eb1 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/ryanhamamura/games/router" "github.com/ryanhamamura/games/sessions" "github.com/ryanhamamura/games/snake" + "github.com/ryanhamamura/games/version" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -45,7 +46,7 @@ func main() { func run(ctx context.Context) error { cfg := config.Global addr := fmt.Sprintf("%s:%s", cfg.Host, cfg.Port) - slog.Info("server starting", "addr", addr) + slog.Info("server starting", "addr", addr, "version", version.Version, "commit", version.Commit) defer slog.Info("server shutdown complete") eg, egctx := errgroup.WithContext(ctx) diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..6ce8a01 --- /dev/null +++ b/version/version.go @@ -0,0 +1,10 @@ +// Package version holds build-time version information injected via ldflags. +package version + +// Version and Commit are set at build time via: +// +// -ldflags "-X github.com/ryanhamamura/games/version.Version=... -X github.com/ryanhamamura/games/version.Commit=..." +var ( + Version = "dev" + Commit = "unknown" +)