Easy versioning for your Go binaries
💥 NOTE: You can use this as a library, but it's so small that you probably shouldn't. Instead, just copy/paste from this gist 💥
This library, along with the following instructions, add the following flags to your Go binary:
--rev: prints the git revision used to build binary--version: prints the latest version, determined either by tags or the latest commit hash
-
Import the package into any of your
.gofilesimport "github.com/rafecolton/versioning"
-
Add the command line args by adding the following to your
init()function in the same.gofileversioning.Parse()
NOTE: It is not required that this call be made in your
init()function, but it is strongly recommended. For it to work correctly, it must be made before your call toflag.Parse()(or similar), otherwiseflagwill explode due to unrecognized args. -
Add the
ldflagsto yourMakefileThis step is the secret sauce that actually makes this work. First, add the vars you'll need.
REV_VAR := github.com/rafecolton/versioning.RevString
VERSION_VAR := github.com/rafecolton/versioning.VersionString
REPO_VERSION := $(shell git describe --always --dirty --tags)
REPO_REV := $(shell git rev-parse --sq HEAD)
GOBUILD_VERSION_ARGS := -ldflags "-X $(REV_VAR) $(REPO_REV) -X $(VERSION_VAR) $(REPO_VERSION)"Then, add the args to your go install command. For example,
go install -x $(TARGETS)becomes
go install $(GOBUILD_VERSION_ARGS) -x $(TARGETS)-
Don't forget to include a
go getcommand to pull down the required library. -
makeand enjoy!