Stamp the git tag or short commit in a plain docker build (closes #86) #87

Merged
clawbot merged 1 commits from issue-86-docker-version-from-git into next 2026-10-02 04:26:41 +02:00
4 changed files with 23 additions and 9 deletions
Showing only changes of commit 5419a7357a - Show all commits
+3
View File
@@ -4,3 +4,6 @@ tmp
.DS_Store .DS_Store
*.log *.log
.claude .claude
# .git is sent so the build can stamp the version, without its config.
.git/config
+16 -3
View File
@@ -20,7 +20,7 @@ RUN make lint
# golang:1.25-alpine (2026-02-27) # golang:1.25-alpine (2026-02-27)
FROM golang:1.25-alpine@sha256:f6751d823c26342f9506c03797d2527668d095b0a15f1862cddb4d927a7a4ced AS builder FROM golang:1.25-alpine@sha256:f6751d823c26342f9506c03797d2527668d095b0a15f1862cddb4d927a7a4ced AS builder
RUN apk add --no-cache make RUN apk add --no-cache git make
WORKDIR /src WORKDIR /src
@@ -40,8 +40,21 @@ RUN make test
# CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.Version=..." # CGO_ENABLED=0 go build -trimpath -ldflags "-s -w -X main.Version=..."
# That script reads VERSION from the environment, so it is handed over # That script reads VERSION from the environment, so it is handed over
# there rather than as a make variable. # there rather than as a make variable.
ARG VERSION=dev #
RUN VERSION="${VERSION}" make build # The version is the VERSION build argument when one is given, otherwise
# `git describe --tags --always` of the repo's .git: the tag on a tagged
# commit, tag-N-gHASH on a commit after one, the short commit when no
# tag is reachable. A version that still comes out empty, dev or unknown
# fails the build. .git goes to /git, not /src/.git, where go build would
# find it and record VCS details of a work tree holding only backend/.
COPY .git /git
ARG VERSION
RUN version="${VERSION:-$(git --git-dir=/git describe --tags --always)}"; \
case "$version" in ""|dev|unknown) \
echo "version is '$version' although .git is present" >&2; \
exit 1 ;; \
esac; \
VERSION="$version" make build
# Frontend stage # Frontend stage
# node:22-alpine as of 2026-02-22 # node:22-alpine as of 2026-02-22
+2 -3
View File
@@ -16,9 +16,8 @@ main() {
"$SCRIPT_DIR/check" "$SCRIPT_DIR/check"
# Own line: a failing command substitution inside an argument does # Own line: a failing command substitution inside an argument does
# not trip `set -e`, so the inline form degrades silently to an # not trip `set -e`, so the inline form degrades silently to an
# empty constant. VERSION is computed here because .dockerignore # empty constant. The VERSION build argument takes precedence over
# excludes .git, so `git describe` in a build stage yields an empty # the version a build stage derives from the .git in the context.
# version without failing.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build --no-cache \ docker build --no-cache \
+2 -3
View File
@@ -12,9 +12,8 @@ main() {
cd "$ROOT" cd "$ROOT"
# Own line: a failing command substitution inside an argument does # Own line: a failing command substitution inside an argument does
# not trip `set -e`, so the inline form degrades silently to an # not trip `set -e`, so the inline form degrades silently to an
# empty constant. VERSION is computed here because .dockerignore # empty constant. The VERSION build argument takes precedence over
# excludes .git, so `git describe` in a build stage yields an empty # the version a build stage derives from the .git in the context.
# version without failing.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build --no-cache \ docker build --no-cache \