From 3b971e3ab83356a60e05b62c7aca263eb9760634 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 7 Jan 2025 16:10:09 +0100 Subject: [PATCH] Replace commit.txt with ldflags value --- .github/workflows/test-build.yml | 1 - .gitignore | 1 - build/package/Dockerfile | 2 +- tracing/tracing.go | 12 +++++++++--- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test-build.yml b/.github/workflows/test-build.yml index 10ecbac5..b4be4483 100644 --- a/.github/workflows/test-build.yml +++ b/.github/workflows/test-build.yml @@ -35,7 +35,6 @@ jobs: - name: Go Generate run: | go generate ./... - cat tracing/commit.txt - name: Validate Docs Data run: | diff --git a/.gitignore b/.gitignore index 730ddcf3..eb0a03cb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,5 @@ vendor Gopkg.lock build/compiled .idea -commit.txt .envrc .golangci.yml diff --git a/build/package/Dockerfile b/build/package/Dockerfile index 29bb750e..93c21d88 100644 --- a/build/package/Dockerfile +++ b/build/package/Dockerfile @@ -23,7 +23,7 @@ COPY . . RUN --mount=type=cache,target=/go/pkg \ --mount=type=cache,target=/root/.cache/go-build \ go generate ./... \ - && GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-s -w" -trimpath -o source main.go + && GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-s -w -X github.com/overmindtech/aws-source/tracing.ServiceVersion=$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)" -trimpath -o source main.go FROM alpine:3.21 WORKDIR / diff --git a/tracing/tracing.go b/tracing/tracing.go index 6ff42a00..fa371b71 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -22,9 +22,15 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.26.0" ) -//go:generate sh -c "echo -n $(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD) > commit.txt" -//go:embed commit.txt -var ServiceVersion string +// ServiceVersion is the version of the service. This will be overridden by the +// build system, using: +// go build -ldflags "-X github.com/overmindtech/api-server/tracing.ServiceVersion=$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)" -o your-app +// +// This allows our change detection workflow to work correctly. If we were +// embedding the version here each time we would always produce a slightly +// different compiled binary, and therefore it would look like there was a +// change each time +var ServiceVersion = "dev" func tracingResource() *resource.Resource { // Identify your application using resource detection