Skip to content

Commit

Permalink
build: Add versioning
Browse files Browse the repository at this point in the history
This handles:
- builds from a git repository
- builds from a tarball
- builds using `go install ...`
  • Loading branch information
cfergeau committed Jun 13, 2024
1 parent 0986d5f commit 2106f4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/pkg/cmdline/version.go export-subst
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.PHONY: all build check clean cross test

GIT_VERSION ?= $(shell git describe --always --dirty)
VERSION_LDFLAGS=-X github.com/crc-org/macadam/pkg/cmdline.gitVersion=$(GIT_VERSION)

DEFAULT_GOOS=$(shell go env GOOS)
DEFAULT_GOARCH=$(shell go env GOARCH)

Expand All @@ -23,27 +26,27 @@ clean:
bin/macadam-darwin-amd64: GOOS=darwin
bin/macadam-darwin-amd64: GOARCH=amd64
bin/macadam-darwin-amd64: force-build
@go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam
@go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam

bin/macadam-darwin-arm64: GOOS=darwin
bin/macadam-darwin-arm64: GOARCH=arm64
bin/macadam-darwin-arm64: force-build
@go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam
@go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam

bin/macadam-linux-amd64: GOOS=linux
bin/macadam-linux-amd64: GOARCH=amd64
bin/macadam-linux-amd64: force-build
@go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam
@go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam

bin/macadam-linux-arm64: GOOS=linux
bin/macadam-linux-arm64: GOARCH=arm64
bin/macadam-linux-arm64: force-build
@go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam
@go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam

bin/macadam-windows-amd64: GOOS=windows
bin/macadam-windows-amd64: GOARCH=amd64
bin/macadam-windows-amd64: force-build
@go build -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam
@go build -ldflags "$(VERSION_LDFLAGS)" -o bin/macadam-$(GOOS)-$(GOARCH) ./cmd/macadam

.PHONY: lint
lint: $(TOOLS_BINDIR)/golangci-lint
Expand Down
5 changes: 4 additions & 1 deletion cmd/macadam/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"fmt"
"log/slog"

"github.com/crc-org/macadam/pkg/cmdline"
)

func main() {
slog.Info("macadam")
slog.Info(fmt.Sprintf("macadam version %s", cmdline.Version()))
}
40 changes: 40 additions & 0 deletions pkg/cmdline/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmdline

import (
"runtime/debug"
"strings"
)

var (
// set using the '-X github.com/crc-org/macadam/pkg/cmdline.gitVersion' linker flag
gitVersion = "unknown"

// set through .gitattributes when `git archive` is used
// see https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/
gitArchiveVersion = "$Format:%(describe)$"
)

func Version() string {
switch {
// This will be substituted when building from a GitHub tarball
case !strings.HasPrefix(gitArchiveVersion, "$Format:"):
return gitArchiveVersion
// This will be set when building from git using make
case gitVersion != "":
return gitVersion
// moduleVersionFromBuildInfo() will be set when using `go install`
default:
return moduleVersionFromBuildInfo()
}
}

func moduleVersionFromBuildInfo() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
if info.Main.Version == "(devel)" {
return ""
}
return info.Main.Version
}

0 comments on commit 2106f4e

Please sign in to comment.