Skip to content

Commit 2a5e63c

Browse files
committed
Embed version info into released binaries
1 parent 6bafc93 commit 2a5e63c

File tree

9 files changed

+3260
-7
lines changed

9 files changed

+3260
-7
lines changed

.bingo/Variables.mk

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ $(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
4747
@echo "(re)installing $(GOBIN)/golangci-lint-v1.51.2"
4848
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=golangci-lint.mod -o=$(GOBIN)/golangci-lint-v1.51.2 "github.com/golangci/golangci-lint/cmd/golangci-lint"
4949

50-
GORELEASER := $(GOBIN)/goreleaser-v0.183.0
50+
GORELEASER := $(GOBIN)/goreleaser-v1.15.2
5151
$(GORELEASER): $(BINGO_DIR)/goreleaser.mod
5252
@# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies.
53-
@echo "(re)installing $(GOBIN)/goreleaser-v0.183.0"
54-
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=goreleaser.mod -o=$(GOBIN)/goreleaser-v0.183.0 "github.com/goreleaser/goreleaser"
53+
@echo "(re)installing $(GOBIN)/goreleaser-v1.15.2"
54+
@cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=goreleaser.mod -o=$(GOBIN)/goreleaser-v1.15.2 "github.com/goreleaser/goreleaser"
5555

5656
GOTEST2ACTION := $(GOBIN)/gotest2action-v0.0.0
5757
$(GOTEST2ACTION): $(BINGO_DIR)/gotest2action.mod

.bingo/goreleaser.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
22

33
go 1.20
44

5-
require github.com/goreleaser/goreleaser v0.183.0
5+
require github.com/goreleaser/goreleaser v1.15.2

.bingo/goreleaser.sum

+3,165
Large diffs are not rendered by default.

.bingo/variables.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ GOIMPORTS="${GOBIN}/goimports-v0.1.7"
1818

1919
GOLANGCI_LINT="${GOBIN}/golangci-lint-v1.51.2"
2020

21-
GORELEASER="${GOBIN}/goreleaser-v0.183.0"
21+
GORELEASER="${GOBIN}/goreleaser-v1.15.2"
2222

2323
GOTEST2ACTION="${GOBIN}/gotest2action-v0.0.0"
2424

.github/workflows/release.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ jobs:
2929
username: ${{ secrets.DOCKERHUB_USERNAME }}
3030
password: ${{ secrets.DOCKERHUB_TOKEN }}
3131

32+
- name: Set Variables
33+
run: |
34+
echo "HOSTNAME=$(hostname)" >> $GITHUB_ENV
35+
3236
- name: Run GoReleaser
3337
uses: goreleaser/goreleaser-action@v2
3438
with:
3539
distribution: goreleaser
3640
version: latest
37-
args: release --rm-dist
41+
args: release --clean
3842
env:
3943
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.goreleaser.yml

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ builds:
1717
- linux
1818
- windows
1919
- darwin
20+
ldflags:
21+
- -s
22+
- -w
23+
- -X github.com/MacroPower/go_template/internal/version.Version={{.Version}}
24+
- -X github.com/MacroPower/go_template/internal/version.Branch={{.Branch}}
25+
- -X github.com/MacroPower/go_template/internal/version.BuildUser={{.Env.USER}}@{{.Env.HOSTNAME}}
26+
- -X github.com/MacroPower/go_template/internal/version.BuildDate={{.Date}}
2027

2128
archives:
2229
- replacements:

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ go-bench: $(BENCHSTAT)
7070

7171
.PHONY: go-build
7272
go-build: ## Builds Go executables.
73+
go-build: HOSTNAME:=$(shell hostname)
7374
go-build: $(GORELEASER)
7475
@echo ">> building Go executables"
75-
$(GORELEASER) build --snapshot --rm-dist
76+
HOSTNAME=$(HOSTNAME) $(GORELEASER) build --snapshot --clean

cmd/go_template/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/MacroPower/go_template/internal/log"
9+
"github.com/MacroPower/go_template/internal/version"
910

1011
"github.com/alecthomas/kong"
1112
)
@@ -39,6 +40,8 @@ func main() {
3940

4041
err := log.Info(logger).Log("msg", fmt.Sprintf("Starting %s", appName))
4142
cliCtx.FatalIfErrorf(err)
43+
version.LogInfo(logger)
44+
version.LogBuildContext(logger)
4245

4346
sb := strings.Builder{}
4447
Hello(&sb)

internal/version/version.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package version
2+
3+
import (
4+
"runtime"
5+
"runtime/debug"
6+
7+
"github.com/MacroPower/go_template/internal/log"
8+
9+
kitlog "github.com/go-kit/log"
10+
)
11+
12+
var (
13+
Version string // Set via ldflags.
14+
Branch string
15+
BuildUser string
16+
BuildDate string
17+
18+
Revision = getRevision()
19+
GoVersion = runtime.Version()
20+
GoOS = runtime.GOOS
21+
GoArch = runtime.GOARCH
22+
)
23+
24+
// LogInfo logs version, branch and revision.
25+
func LogInfo(logger kitlog.Logger) {
26+
if err := log.Info(logger).Log(
27+
"msg", "info",
28+
"version", Version,
29+
"branch", Branch,
30+
"revision", Revision,
31+
); err != nil {
32+
panic(err)
33+
}
34+
}
35+
36+
// LogBuildContext logs goVersion, platform, buildUser and buildDate.
37+
func LogBuildContext(logger kitlog.Logger) {
38+
if err := log.Info(logger).Log(
39+
"msg", "build context",
40+
"go", GoVersion,
41+
"platform", GoOS+"/"+GoArch,
42+
"user", BuildUser,
43+
"date", BuildDate,
44+
); err != nil {
45+
panic(err)
46+
}
47+
}
48+
49+
func getRevision() string {
50+
rev := "unknown"
51+
52+
buildInfo, ok := debug.ReadBuildInfo()
53+
if !ok {
54+
return rev
55+
}
56+
57+
modified := false
58+
for _, v := range buildInfo.Settings {
59+
switch v.Key {
60+
case "vcs.revision":
61+
rev = v.Value
62+
case "vcs.modified":
63+
if v.Value == "true" {
64+
modified = true
65+
}
66+
}
67+
}
68+
if modified {
69+
return rev + "-dirty"
70+
}
71+
72+
return rev
73+
}

0 commit comments

Comments
 (0)