Skip to content

Commit

Permalink
Embed version info into released binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
MacroPower committed Mar 2, 2023
1 parent 6bafc93 commit 2a5e63c
Show file tree
Hide file tree
Showing 9 changed files with 3,260 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .bingo/Variables.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ $(GOLANGCI_LINT): $(BINGO_DIR)/golangci-lint.mod
@echo "(re)installing $(GOBIN)/golangci-lint-v1.51.2"
@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"

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

GOTEST2ACTION := $(GOBIN)/gotest2action-v0.0.0
$(GOTEST2ACTION): $(BINGO_DIR)/gotest2action.mod
Expand Down
2 changes: 1 addition & 1 deletion .bingo/goreleaser.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT

go 1.20

require github.com/goreleaser/goreleaser v0.183.0
require github.com/goreleaser/goreleaser v1.15.2
3,165 changes: 3,165 additions & 0 deletions .bingo/goreleaser.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .bingo/variables.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GOIMPORTS="${GOBIN}/goimports-v0.1.7"

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

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

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

6 changes: 5 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Set Variables
run: |
echo "HOSTNAME=$(hostname)" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 7 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s
- -w
- -X github.com/MacroPower/go_template/internal/version.Version={{.Version}}
- -X github.com/MacroPower/go_template/internal/version.Branch={{.Branch}}
- -X github.com/MacroPower/go_template/internal/version.BuildUser={{.Env.USER}}@{{.Env.HOSTNAME}}
- -X github.com/MacroPower/go_template/internal/version.BuildDate={{.Date}}

archives:
- replacements:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ go-bench: $(BENCHSTAT)

.PHONY: go-build
go-build: ## Builds Go executables.
go-build: HOSTNAME:=$(shell hostname)
go-build: $(GORELEASER)
@echo ">> building Go executables"
$(GORELEASER) build --snapshot --rm-dist
HOSTNAME=$(HOSTNAME) $(GORELEASER) build --snapshot --clean
3 changes: 3 additions & 0 deletions cmd/go_template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/MacroPower/go_template/internal/log"
"github.com/MacroPower/go_template/internal/version"

"github.com/alecthomas/kong"
)
Expand Down Expand Up @@ -39,6 +40,8 @@ func main() {

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

sb := strings.Builder{}
Hello(&sb)
Expand Down
73 changes: 73 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package version

import (
"runtime"
"runtime/debug"

"github.com/MacroPower/go_template/internal/log"

kitlog "github.com/go-kit/log"
)

var (
Version string // Set via ldflags.
Branch string
BuildUser string
BuildDate string

Revision = getRevision()
GoVersion = runtime.Version()
GoOS = runtime.GOOS
GoArch = runtime.GOARCH
)

// LogInfo logs version, branch and revision.
func LogInfo(logger kitlog.Logger) {
if err := log.Info(logger).Log(
"msg", "info",
"version", Version,
"branch", Branch,
"revision", Revision,
); err != nil {
panic(err)
}
}

// LogBuildContext logs goVersion, platform, buildUser and buildDate.
func LogBuildContext(logger kitlog.Logger) {
if err := log.Info(logger).Log(
"msg", "build context",
"go", GoVersion,
"platform", GoOS+"/"+GoArch,
"user", BuildUser,
"date", BuildDate,
); err != nil {
panic(err)
}
}

func getRevision() string {
rev := "unknown"

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return rev
}

modified := false
for _, v := range buildInfo.Settings {
switch v.Key {
case "vcs.revision":
rev = v.Value
case "vcs.modified":
if v.Value == "true" {
modified = true
}
}
}
if modified {
return rev + "-dirty"
}

return rev
}

0 comments on commit 2a5e63c

Please sign in to comment.