简体   繁体   中英

Go: how to add git revision to binaries built?

I want to add the current git revision number to the the binary built by go build so that I can do something like ./mybinary --revision to see which revision it is built from (usually for troubleshooting later on after deployment).

Obviously I cannot put the revision number into the source since that will change the source with a new revision.

I'm wondering if there is any other way to do this?
Or do you think this is just a bad idea? If so, what's the recommended way to establish the relation between built binaries and its source version?
Version numbers do not seem to be a good idea with a distributed version control system.

If you can get the git revision into $VERSION and have a variable named version (type string) in your main package, you can set it during the build with:

#!/bin/sh
VERSION=`git rev-parse --short HEAD`
go build -ldflags "-X main.version=$VERSION"  myfile.go

For all and any versioned in Git code most obvious way for getting identification-string for any changeset (while I leave to you task of displaying this string on --revision options) is

  • using (at least sporadically) tags
  • git describe (with relevant options) on build stage

I'd create a version.go file with a single var version string and then process that before a call to go build and reset it after. In other words, go doesn't support any type of code generation so you'll need to rely on something external to do this.

You would generally use tags (also known as labels in other version control systems) to note the files that make up a particular build. http://git-scm.com/book/en/Git-Basics-Tagging . I typically make tags that include the version and build date. For example v1.2_29Mar2013. If I have several products that can be built from the same code base I'll include something to identify which product.

The normal process should no longer involve any ldflags , starting with Go 1.18 (Q4 2021/Q1 2022).

See issue 37475 and CL 356013

The go command now embeds version control information in binaries including the currently checked-out revision and a flag indicating whether edited or untracked files are present.

Version control information is embedded if the go command is invoked in a directory within a Git or Mercurial repository, and the main package and its containing main module are in the same repository.
This information may be omitted using the flag -buildvcs=false .

Additionally, the go command embeds information about:

  • the build including build and tool tags (set with -tags ),
  • compiler,
  • assembler, and
  • linker flags (like -gcflags ),
  • whether cgo was enabled, and if it was, the values of the cgo environment variables (like CGO_CFLAGS ).

This information may be omitted using the flag -buildinfo=false .

Both VCS and build information may be read together with module information using:

  • go version -m file or
  • runtime/debug.ReadBuildInfo (for the currently running binary) or
  • the new debug/buildinfo package .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM