Go to: how to add git revision to generated binaries?

I want to add the current git version number to the binary built with go build so that I can do something like ./mybinary --revision to see which version it is built from (usually to troubleshoot later after deployment ), Obviously, I cannot put the version number in the source, as this will change the source code with the new version. I am wondering if there is another way to do this? Or do you think this is just a bad idea? If so, what is the recommended way to establish a connection between the embedded binaries and the original version? Version numbers do not seem to be a good idea in a distributed version control system.

+4
source share
4 answers

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

 #!/bin/sh VERSION=`git log | head -n 1 | cut -f 2 -d ' '` go build -ldflags "-X main.version=$VERSION" myfile.go 
+7
source

For all and any versions in Git code, the most obvious way to get an identification string for any set of changes (for now I leave you the task of displaying this string in the --revision options)

  • using (at least sporadically) tags
  • describe git (with appropriate parameters) at the build stage
+2
source

I would create a version.go file with one var version string , and then process it before calling go build and reset after it. In other words, go does not support any type of code generation, so you will need to rely on something external to do this.

+2
source

You typically used tags (also called tags in other version control systems) to mark files that make up a particular assembly. http://git-scm.com/book/en/Git-Basics-Tagging . I usually 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 will include something to determine which product.

+1
source

All Articles