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
.go
filesimport "github.com/rafecolton/versioning"
-
Add the command line args by adding the following to your
init()
function in the same.go
fileversioning.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), otherwiseflag
will explode due to unrecognized args. -
Add the
ldflags
to yourMakefile
This 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 get
command to pull down the required library. -
make
and enjoy!