From d3a0d22505e4e47d84e8b8fcc48302048394c8b6 Mon Sep 17 00:00:00 2001 From: Khosrow Moossavi Date: Wed, 29 May 2024 11:37:11 -0400 Subject: [PATCH] feat(version): append commit hash on build via ldflags It standardize resonate binary version to: resonate version v[--][ COMMIT_HASH] / where: - `VERSION`: the release version - `PRE_RELEASE`: can be any arbitrary string, e.g. `alpha`, `beta`, `rc.1`, etc - `TIMESTAMP`: the yyyymmdd of build IF this is not a GA release - `COMMIT_HASH`: short revision IF this is a GA release - `GOOS`: underlying OS this binary built on - `GOARCH`: underlying Architecture this binary built on This also adds a new `version` subcommand for convenience and to not give an error on `resonate version` for folks with muscle memory for it. Signed-off-by: Khosrow Moossavi --- .../release_publish_github_image.yaml | 2 + Dockerfile | 9 ++++- cmd/root.go | 5 ++- cmd/version/version.go | 20 ++++++++++ flake.nix | 5 ++- internal/version/version.go | 39 +++++++++++++++++++ 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 cmd/version/version.go create mode 100644 internal/version/version.go diff --git a/.github/workflows/release_publish_github_image.yaml b/.github/workflows/release_publish_github_image.yaml index 47a4fffc..d8b82b70 100644 --- a/.github/workflows/release_publish_github_image.yaml +++ b/.github/workflows/release_publish_github_image.yaml @@ -65,6 +65,8 @@ jobs: uses: docker/build-push-action@v5 with: context: . + build-args: | + COMMIT_HASH="${{ github.sha }}" platforms: linux/amd64,linux/arm64 push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile index b3b8e985..c8b5972c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,17 @@ # Build. FROM cgr.dev/chainguard/go AS builder +ARG COMMIT_HASH + WORKDIR /app COPY . . -RUN CGO_ENABLED=1 GOOS=linux go build -o resonate . +RUN CGO_ENABLED=1 \ + GOOS=linux \ + go \ + build \ + -ldflags="-X github.com/resonatehq/resonate/internal/version.commit=${COMMIT_HASH}" \ + -o resonate . # Distribute. FROM cgr.dev/chainguard/glibc-dynamic diff --git a/cmd/root.go b/cmd/root.go index 238d52d4..b120b373 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,8 @@ import ( "github.com/resonatehq/resonate/cmd/quickstart" "github.com/resonatehq/resonate/cmd/schedules" "github.com/resonatehq/resonate/cmd/serve" + versioncmd "github.com/resonatehq/resonate/cmd/version" + "github.com/resonatehq/resonate/internal/version" "github.com/resonatehq/resonate/pkg/client" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -22,7 +24,7 @@ var ( var rootCmd = &cobra.Command{ Use: "resonate", Short: "Durable promises", - Version: "0.5.6", // This needs to be bumped when new versions are released. + Version: version.Full(), } func init() { @@ -43,6 +45,7 @@ func init() { rootCmd.AddCommand(dst.NewCmd()) rootCmd.AddCommand(serve.ServeCmd()) rootCmd.AddCommand(quickstart.NewCmd()) + rootCmd.AddCommand(versioncmd.NewCmd()) // Set default output rootCmd.SetOut(os.Stdout) diff --git a/cmd/version/version.go b/cmd/version/version.go new file mode 100644 index 00000000..b96c3b4f --- /dev/null +++ b/cmd/version/version.go @@ -0,0 +1,20 @@ +package version + +import ( + "fmt" + + "github.com/resonatehq/resonate/internal/version" + "github.com/spf13/cobra" +) + +func NewCmd() *cobra.Command { + cmd := &cobra.Command{ + Args: cobra.NoArgs, + Use: "version", + Short: "Print the version for resonate", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("resonate version %s\n", version.Full()) + }, + } + return cmd +} diff --git a/flake.nix b/flake.nix index 6486251c..2fb20134 100644 --- a/flake.nix +++ b/flake.nix @@ -18,7 +18,8 @@ let # Version inference lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101"; - version = "${builtins.substring 0 8 lastModifiedDate}-${self.shortRev or "dirty"}"; + shortRev = "${self.shortRev or "dirty"}"; + version = "${builtins.substring 0 8 lastModifiedDate}-${shortRev}"; # Helpers for producing system-specific outputs supportedSystems = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" "aarch64-linux" ]; @@ -77,6 +78,7 @@ # The Resonate server resonate = pkgs.buildGoApplication rec { pname = "resonate"; + inherit shortRev; inherit version; src = self; modules = ./gomod2nix.toml; @@ -88,6 +90,7 @@ ldflags = [ "-s" "-w" + "-X github.com/resonatehq/resonate/internal/version.commit=$(shortRev)" ] ++ pkgs.lib.optional (pkgs.stdenv.isLinux) [ "-extldflags=-static" "-linkmode=external" diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..3d455ab8 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,39 @@ +package version + +import ( + "fmt" + "runtime" + "time" +) + +// current version +const ( + coreVersion = "0.5.7" + prerelease = "alpha" +) + +// Provisioned by ldflags +var commit string + +// Core return the core version. +func Core() string { + return coreVersion +} + +// Short return the version with pre-release, if available. +func Short() string { + if prerelease != "" { + return fmt.Sprintf("%s-%s-%s", coreVersion, prerelease, time.Now().Format("20060102")) + } + + return coreVersion +} + +// Full return the full version including pre-release, commit hash, runtime os and arch. +func Full() string { + if commit != "" && commit[:1] != " " { + commit = " " + commit + } + + return fmt.Sprintf("v%s%s %s/%s", Short(), commit, runtime.GOOS, runtime.GOARCH) +}