Skip to content

Commit e47b8d0

Browse files
authored
Merge pull request #243 from c3-oss/fix/docker-buildinfo
fix(release): inject buildinfo into Docker image binaries
2 parents 44480c1 + 0a4d9b0 commit e47b8d0

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

.github/workflows/release.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ jobs:
8383
# stage via buildx cache. Each image is published at
8484
# ghcr.io/<owner>/<binary> with the release tag and `latest`.
8585

86+
# Mirrors GoReleaser's {{.Version}} / {{.Commit}} / {{.Date}} so
87+
# binaries inside the images report the same buildinfo as the
88+
# tarball binaries.
89+
- name: Compute Docker build metadata
90+
run: |
91+
{
92+
echo "BUILD_VERSION=${GITHUB_REF_NAME#v}"
93+
echo "BUILD_COMMIT=${GITHUB_SHA}"
94+
echo "BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
95+
} >> "$GITHUB_ENV"
96+
8697
- name: Docker meta — prosa (CLI)
8798
id: meta-cli
8899
uses: docker/metadata-action@v6
@@ -101,6 +112,10 @@ jobs:
101112
push: true
102113
tags: ${{ steps.meta-cli.outputs.tags }}
103114
labels: ${{ steps.meta-cli.outputs.labels }}
115+
build-args: |
116+
VERSION=${{ env.BUILD_VERSION }}
117+
COMMIT=${{ env.BUILD_COMMIT }}
118+
BUILD_DATE=${{ env.BUILD_DATE }}
104119
105120
- name: Docker meta — prosa-server
106121
id: meta-server
@@ -120,6 +135,10 @@ jobs:
120135
push: true
121136
tags: ${{ steps.meta-server.outputs.tags }}
122137
labels: ${{ steps.meta-server.outputs.labels }}
138+
build-args: |
139+
VERSION=${{ env.BUILD_VERSION }}
140+
COMMIT=${{ env.BUILD_COMMIT }}
141+
BUILD_DATE=${{ env.BUILD_DATE }}
123142
124143
- name: Docker meta — prosa-panel
125144
id: meta-panel
@@ -139,3 +158,7 @@ jobs:
139158
push: true
140159
tags: ${{ steps.meta-panel.outputs.tags }}
141160
labels: ${{ steps.meta-panel.outputs.labels }}
161+
build-args: |
162+
VERSION=${{ env.BUILD_VERSION }}
163+
COMMIT=${{ env.BUILD_COMMIT }}
164+
BUILD_DATE=${{ env.BUILD_DATE }}

Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ ARG GO_VERSION=1.26.2
44
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-bookworm AS build
55
ARG TARGETOS
66
ARG TARGETARCH
7+
# Build metadata mirroring the GoReleaser ldflags so image binaries
8+
# report a real version instead of "dev (none, unknown)".
9+
ARG VERSION=dev
10+
ARG COMMIT=none
11+
ARG BUILD_DATE=unknown
712
WORKDIR /src
813
COPY go.mod go.sum ./
914
RUN go mod download
1015
COPY . .
1116
RUN mkdir -p /out && CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
12-
go build -ldflags='-s -w' -o /out/ ./cmd/...
17+
go build -ldflags="-s -w \
18+
-X github.com/c3-oss/prosa/internal/buildinfo.Version=${VERSION} \
19+
-X github.com/c3-oss/prosa/internal/buildinfo.Commit=${COMMIT} \
20+
-X github.com/c3-oss/prosa/internal/buildinfo.BuildDate=${BUILD_DATE}" \
21+
-o /out/ ./cmd/...
1322

1423
FROM gcr.io/distroless/static-debian12 AS prosa
1524
COPY --from=build /out/prosa /usr/local/bin/prosa

docs/distribution/docker.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ ARG GO_VERSION=1.26.2
3131
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-bookworm AS build
3232
ARG TARGETOS
3333
ARG TARGETARCH
34+
# Build metadata mirroring the GoReleaser ldflags so image binaries
35+
# report a real version instead of "dev (none, unknown)".
36+
ARG VERSION=dev
37+
ARG COMMIT=none
38+
ARG BUILD_DATE=unknown
3439
WORKDIR /src
3540
COPY go.mod go.sum ./
3641
RUN go mod download
3742
COPY . .
3843
RUN mkdir -p /out && CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
39-
go build -ldflags='-s -w' -o /out/ ./cmd/...
44+
go build -ldflags="-s -w \
45+
-X github.com/c3-oss/prosa/internal/buildinfo.Version=${VERSION} \
46+
-X github.com/c3-oss/prosa/internal/buildinfo.Commit=${COMMIT} \
47+
-X github.com/c3-oss/prosa/internal/buildinfo.BuildDate=${BUILD_DATE}" \
48+
-o /out/ ./cmd/...
4049

4150
FROM gcr.io/distroless/static-debian12 AS prosa
4251
COPY --from=build /out/prosa /usr/local/bin/prosa
@@ -58,6 +67,10 @@ Notes:
5867
`--target` invocations.
5968
- `CGO_ENABLED=0` keeps the binaries static — distroless has no glibc.
6069
- `-s -w` strips debug symbols.
70+
- The release workflow passes `VERSION` / `COMMIT` / `BUILD_DATE` build
71+
args (tag without the `v` prefix, commit SHA, UTC RFC 3339 date) so
72+
`--version` and startup logs match the tarball binaries. Local builds
73+
without the args report `dev (none, unknown)`.
6174
- `--platform=$BUILDPLATFORM` lets Buildx cross-compile from one
6275
builder; `TARGETOS`/`TARGETARCH` come from
6376
`docker buildx build --platform`.

0 commit comments

Comments
 (0)