简体   繁体   English

Go:如何将 git 修订版添加到构建的二进制文件中?

[英]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).我想将当前的 git 修订号添加到由go build的二进制文件中,以便我可以执行类似./mybinary --revision以查看它是从哪个修订版构建的(通常用于在部署后进行故障排除)。

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:如果您可以将 git 修订版放入 $VERSION 并在主包中有一个名为version (类型字符串)的变量,则可以在构建期间使用以下命令设置它:

#!/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对于所有和任何版本的 Git 代码,获取任何变更集的标识字符串的最明显方法(而我将在--revision选项上显示此字符串的任务留给您)是

  • using (at least sporadically) tags使用(至少偶尔)标签
  • git describe (with relevant options) on build stage git describe (带有相关选项)在构建阶段

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.我会创建一个带有单个var version stringversion.go文件,然后在调用go build之前处理它并在之后重置它。 In other words, go doesn't support any type of code generation so you'll need to rely on something external to do this.换句话说,go 不支持任何类型的代码生成,所以你需要依靠外部的东西来做到这一点。

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 . 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.例如 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).从 Go 1.18(2021 年第 4 季度/2022 年第 1 季度)开始,正常流程不应再涉及任何ldflags

See issue 37475 and CL 356013请参阅问题 37475CL 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. go命令现在将版本控制信息嵌入到二进制文件中,包括当前检出的修订版和指示是否存在已编辑或未跟踪文件的标志。

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.如果在 Git 或 Mercurial 存储库中的目录中调用go命令,并且main包及其包含的main模块在同一个存储库中,则会嵌入版本控制信息。
This information may be omitted using the flag -buildvcs=false .可以使用标志-buildvcs=false省略此信息。

Additionally, the go command embeds information about:此外, go命令嵌入了以下信息:

  • the build including build and tool tags (set with -tags ),构建包括构建和工具标签(使用-tags设置),
  • compiler,编译器,
  • assembler, and汇编程序,和
  • linker flags (like -gcflags ),链接器标志(如-gcflags ),
  • whether cgo was enabled, and if it was, the values of the cgo environment variables (like CGO_CFLAGS ). cgo是否被启用,如果是, cgo环境变量的值(如CGO_CFLAGS )。

This information may be omitted using the flag -buildinfo=false .可以使用标志-buildinfo=false省略此信息。

Both VCS and build information may be read together with module information using: VCS 和构建信息都可以与模块信息一起使用:

  • go version -m file or go version -m file
  • runtime/debug.ReadBuildInfo (for the currently running binary) or runtime/debug.ReadBuildInfo (用于当前运行的二进制文件)或
  • the new debug/buildinfo package .新的debug/buildinfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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