From 1c477fe32ae6550231777e601d4121c646323513 Mon Sep 17 00:00:00 2001 From: Astra Date: Wed, 9 Sep 2026 14:26:58 +0100 Subject: [PATCH] build: stamp git metadata into the container image .containerignore excludes .git, so go build's automatic VCS stamping produced nothing and telesrv logged git_commit/git_branch/git_tree_state/build_time as "unknown" on startup. - Containerfile: accept GIT_COMMIT/GIT_BRANCH/GIT_TREE_STATE/BUILD_TIME build args and pass them to the gramsrv build via -ldflags -X. - build.sh: wrapper that fills those args from the current checkout and runs podman build. --- Containerfile | 17 ++++++++++++++++- build.sh | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100755 build.sh diff --git a/Containerfile b/Containerfile index 57f01c29..dfc6f69b 100644 --- a/Containerfile +++ b/Containerfile @@ -1,7 +1,22 @@ FROM docker.io/library/golang:1.25 AS build +# Build metadata for telesrv's startup log (git_commit/git_branch/... in +# cmd/telesrv/buildinfo.go). .containerignore excludes .git, so go build's +# automatic VCS stamping sees no repo; pass these in explicitly, e.g.: +# podman build \ +# --build-arg GIT_COMMIT="$(git rev-parse HEAD)" \ +# --build-arg GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" \ +# --build-arg GIT_TREE_STATE="$(git diff --quiet && echo clean || echo dirty)" \ +# --build-arg BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ +# -t owpengram-server -f Containerfile . +ARG GIT_COMMIT=unknown +ARG GIT_BRANCH=unknown +ARG GIT_TREE_STATE=unknown +ARG BUILD_TIME=unknown WORKDIR /src COPY . . -RUN CGO_ENABLED=0 go build -trimpath -o /out/gramsrv ./cmd/telesrv +RUN CGO_ENABLED=0 go build -trimpath \ + -ldflags "-X main.gitCommit=${GIT_COMMIT} -X main.gitBranch=${GIT_BRANCH} -X main.gitTreeState=${GIT_TREE_STATE} -X main.buildTime=${BUILD_TIME}" \ + -o /out/gramsrv ./cmd/telesrv RUN CGO_ENABLED=0 go build -trimpath -o /out/telesrv-admin ./cmd/telesrv-admin FROM docker.io/library/alpine:3.20 diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..3a065e43 --- /dev/null +++ b/build.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Build the owpengram-server container image, stamping the current git state +# into the binary (shows up in telesrv's startup log as git_commit/git_branch/ +# git_tree_state/build_time). .containerignore excludes .git, so go build's +# automatic VCS stamping can't see the repo - the values are passed in here. +# +# Usage: ./build.sh [extra podman build args...] +# IMAGE=my/tag ./build.sh # override the image tag (default: owpengram-server) +set -euo pipefail +cd "$(dirname "$0")" + +IMAGE="${IMAGE:-owpengram-server}" + +podman build \ + --build-arg GIT_COMMIT="$(git rev-parse HEAD)" \ + --build-arg GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" \ + --build-arg GIT_TREE_STATE="$(git diff --quiet && echo clean || echo dirty)" \ + --build-arg BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ + -t "$IMAGE" \ + -f Containerfile \ + "$@" \ + .