Inject real build version via ldflags (main.version currently defaults to "dev") #47

Closed
opened 2026-07-03 21:59:17 +00:00 by mathias · 1 comment
Owner

Context

v0.5.2 replaced the hardcoded, drifting "version", "0.1.0" startup log with a -ldflags-overridable var version = "dev". That stops the lie, but the deployed image still logs version=dev because nothing injects the actual tag/sha at build time.

Fix (small)

Wire the version through the existing CD metadata:

  • Dockerfile: ARG VERSION=dev + go build -ldflags "-s -w -X main.version=${VERSION}" ...
  • .gitea/workflows/cd.yml build step: pass --build-arg VERSION=${{ steps.meta.outputs.version-tag || steps.meta.outputs.sha-tag }} to the buildah build invocation (the meta step already computes both a version-tag on v* and a short-sha tag).

Then the running pod's startup log (and any future /version) reports the real build.

Acceptance

  • A tagged build logs version=<tag>; an untagged main build logs the short sha.
  • Local go build still works (defaults to dev).
  • CD build passes.

Refs

  • Follow-up to v0.5.2 housekeeping. cmd/gitea-mcp/main.go version var; .gitea/workflows/cd.yml meta step already has the tags.
## Context v0.5.2 replaced the hardcoded, drifting `"version", "0.1.0"` startup log with a `-ldflags`-overridable `var version = "dev"`. That stops the lie, but the deployed image still logs `version=dev` because nothing injects the actual tag/sha at build time. ## Fix (small) Wire the version through the existing CD metadata: - **Dockerfile**: `ARG VERSION=dev` + `go build -ldflags "-s -w -X main.version=${VERSION}" ...` - **`.gitea/workflows/cd.yml`** build step: pass `--build-arg VERSION=${{ steps.meta.outputs.version-tag || steps.meta.outputs.sha-tag }}` to the `buildah build` invocation (the `meta` step already computes both a version-tag on `v*` and a short-sha tag). Then the running pod's startup log (and any future `/version`) reports the real build. ## Acceptance - [ ] A tagged build logs `version=<tag>`; an untagged main build logs the short sha. - [ ] Local `go build` still works (defaults to `dev`). - [ ] CD build passes. ## Refs - Follow-up to v0.5.2 housekeeping. `cmd/gitea-mcp/main.go` `version` var; `.gitea/workflows/cd.yml` `meta` step already has the tags.
Author
Owner

Done in v0.6.1.

  • Dockerfile: ARG VERSION=dev + go build -ldflags="-s -w -X main.version=${VERSION}".
  • cd.yml build step: --build-arg VERSION=<version-tag on v* builds, else short sha> (the meta step already computes both).

Verified locally that -ldflags -X main.version=… embeds the string into the binary. On a main-branch push the deployed image is the sha-tagged build, so the pod now logs version=<short-sha> instead of dev; a v* tag build logs the tag. Local go build still defaults to dev. task check green (exit 0). Verifying the live pod log after the CD roll.

Done in v0.6.1. - **Dockerfile**: `ARG VERSION=dev` + `go build -ldflags="-s -w -X main.version=${VERSION}"`. - **cd.yml** build step: `--build-arg VERSION=<version-tag on v* builds, else short sha>` (the `meta` step already computes both). Verified locally that `-ldflags -X main.version=…` embeds the string into the binary. On a main-branch push the deployed image is the sha-tagged build, so the pod now logs `version=<short-sha>` instead of `dev`; a `v*` tag build logs the tag. Local `go build` still defaults to `dev`. `task check` green (exit 0). Verifying the live pod log after the CD roll.
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mathias/gitea-mcp#47