From 38ac0a3079d8d0b795248eb68cc898aeec35c001 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 7 Jan 2025 15:53:42 +0100 Subject: [PATCH] Replace commit.txt with ldflags value --- .github/actions/go_init/action.yml | 1 - .gitignore | 2 -- .goreleaser.yaml | 12 +++++------- README.md | 2 +- cmd/explore.go | 5 +++-- cmd/root.go | 6 +----- tracing/main.go | 16 +++++++++++----- 7 files changed, 21 insertions(+), 23 deletions(-) diff --git a/.github/actions/go_init/action.yml b/.github/actions/go_init/action.yml index 90bfbde1..330c0956 100644 --- a/.github/actions/go_init/action.yml +++ b/.github/actions/go_init/action.yml @@ -26,4 +26,3 @@ runs: git status exit 1 fi - cat cmd/commit.txt diff --git a/.gitignore b/.gitignore index bb704fb4..e59d1a3b 100644 --- a/.gitignore +++ b/.gitignore @@ -22,8 +22,6 @@ gon go.work dist/ -tracing/commit.txt -cmd/commit.txt output .DS_Store .terraform diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 75c96162..774f0090 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,12 +1,5 @@ -# This is an example .goreleaser.yml file with some sensible defaults. # Make sure to check the documentation at https://goreleaser.com version: 2 -before: - hooks: - # You may remove this if you don't use go modules. - - go mod tidy - # you may remove this if you don't need go generate - - go generate ./... builds: - binary: overmind id: overmind @@ -15,12 +8,17 @@ builds: goos: - linux - windows + # Set the version where the binary will look it up (ServiceVersion) without ruining the flags goreleaser is setting + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X github.com/overmindtech/cli/tracing.ServiceVersion={{.Version}} - binary: overmind id: overmind-macos env: - CGO_ENABLED=0 goos: - darwin + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser -X github.com/overmindtech/cli/tracing.ServiceVersion={{.Version}} # For now we are going to disable signing MacOS packages. This works on Dylan's # person laptop, but we haven't worked out a way to get this set up in a github diff --git a/README.md b/README.md index 9f1e7a39..4eebc0fc 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ Additionally a community maintained package can be found in the [aur](https://au ### ASDF -Overmind can be instaled using [asdf](https://asdf-vm.com/): +Overmind can be installed using [asdf](https://asdf-vm.com/): ```shell # Add the plugin diff --git a/cmd/explore.go b/cmd/explore.go index 01937cd6..0b9a3868 100644 --- a/cmd/explore.go +++ b/cmd/explore.go @@ -12,6 +12,7 @@ import ( "github.com/google/uuid" "github.com/overmindtech/aws-source/proc" "github.com/overmindtech/cli/tfutils" + "github.com/overmindtech/cli/tracing" "github.com/overmindtech/discovery" "github.com/overmindtech/pterm" "github.com/overmindtech/sdp-go" @@ -62,7 +63,7 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut p.Go(func() (*discovery.Engine, error) { ec := discovery.EngineConfig{ - Version: fmt.Sprintf("cli-%v", cliVersion), + Version: fmt.Sprintf("cli-%v", tracing.ServiceVersion), EngineType: "cli-stdlib", SourceName: fmt.Sprintf("stdlib-source-%v", hostname), SourceUUID: uuid.New(), @@ -131,7 +132,7 @@ func StartLocalSources(ctx context.Context, oi sdp.OvermindInstance, token *oaut } ec := discovery.EngineConfig{ EngineType: "cli-aws", - Version: fmt.Sprintf("cli-%v", cliVersion), + Version: fmt.Sprintf("cli-%v", tracing.ServiceVersion), SourceName: fmt.Sprintf("aws-source-%v", hostname), SourceUUID: uuid.New(), App: oi.ApiUrl.Host, diff --git a/cmd/root.go b/cmd/root.go index 4c0c0fc9..cfb5b19a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,10 +35,6 @@ import ( "golang.org/x/oauth2" ) -//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 cliVersion string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "overmind", @@ -49,7 +45,7 @@ confidence. This CLI will prompt you for authentication using Overmind's OAuth service, however it can also be configured to use an API key by setting the OVM_API_KEY environment variable.`, - Version: cliVersion, + Version: tracing.ServiceVersion, SilenceUsage: true, PreRun: PreRunSetup, } diff --git a/tracing/main.go b/tracing/main.go index 85ff91f3..bfd435e6 100644 --- a/tracing/main.go +++ b/tracing/main.go @@ -21,15 +21,21 @@ import ( "go.opentelemetry.io/otel/trace" ) -//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 instrumentationVersion 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" const instrumentationName = "github.com/overmindtech/cli" var tracer = otel.GetTracerProvider().Tracer( instrumentationName, - trace.WithInstrumentationVersion(instrumentationVersion), + trace.WithInstrumentationVersion(ServiceVersion), trace.WithSchemaURL(semconv.SchemaURL), ) @@ -53,7 +59,7 @@ func tracingResource() *resource.Resource { // Add your own custom attributes to identify your application resource.WithAttributes( semconv.ServiceNameKey.String("overmind-cli"), - semconv.ServiceVersionKey.String(instrumentationVersion), + semconv.ServiceVersionKey.String(ServiceVersion), ), ) if err != nil {