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.

贡献者指南