livebud/bud

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

開放

#332 建立於 2022年11月27日

 (2 則留言) (0 個反應) (0 位負責人)JavaScript (211 個分叉)batch import
help wanted

倉庫指標

星標
 (5,391 顆星)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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.

貢獻者指南