Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(version): append commit hash on build via ldflags #342

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Copy link
Author

@khos2ow khos2ow May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will solve the workflow, but I'm not aware of any other breaking change that this might introduce in the broader resonate landscape, if any. Because effectively resonate --version will always have a leading v and tailing <os>/<arch>, if move forward with this PR.


# 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line (you don't need to inherit this value into the derivation function to use it 😄)

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)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case you'll need to use Nix string interpolation like this:

"-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)
}