Skip to content

Commit

Permalink
feat(version): append commit hash on build via ldflags
Browse files Browse the repository at this point in the history
It standardize resonate binary version to:

resonate version v<VERSION>[-<PRE_RELEASE>-<TIMESTAMP>][ COMMIT_HASH] <GOOS>/<GOARCH>

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 <[email protected]>
  • Loading branch information
khos2ow committed May 29, 2024
1 parent 936c333 commit b2a6bd0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release_check_version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
run: |
# Extract resonate version
RESONATE_VERSION=$(./resonate -v | awk '{print $3}')
RESONATE_VERSION="v${RESONATE_VERSION}"
# Compare versions
if [ "$RESONATE_VERSION" != "$GITHUB_REF_VERSION" ]; then
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release_publish_github_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Expand Down Expand Up @@ -77,6 +78,7 @@
# The Resonate server
resonate = pkgs.buildGoApplication rec {
pname = "resonate";
inherit shortRev;
inherit version;
src = self;
modules = ./gomod2nix.toml;
Expand All @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b2a6bd0

Please sign in to comment.