livebud/bud

feature idea: detect git short version hash during build and embed into binary

Open

#332 opened on Nov 27, 2022

View on GitHub
 (2 comments) (0 reactions) (0 assignees)JavaScript (211 forks)batch import
help wanted

Repository metrics

Stars
 (5,391 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

In my apps I usually do something like:

var Version   string

func main() {
	os.Exit(httpserver.Run(Version))
}

Then at build time (in my Makefile) I do this:

VERSION := $(shell git describe --always --dirty=-dirty)
GOLDFLAGS += -X main.Version=$(VERSION)
GOFLAGS := -ldflags "$(GOLDFLAGS)"

This embeds the version string into the app so that I can access it, which is useful for me because then in production I can access this value and put it in my footers and error messages so that I know precisely which commit is running.

It would be cool if bud build could detect if it's building out of a git repo, and embed the git shorthash into the built binary in this way. It seems that you could skip the ldflags part and simply "hardcode" the git shorthash into the main.go via the main.gotext template at build filesystem generation time.

Anyway, just an idea, and I'm happy to take a crack at making a patch for this when I have some time, I just wanted to write it down while it's in my head.

Contributor guide