Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Include GIT SHA in the binary builds #169

Closed
rshriram opened this issue Feb 17, 2017 · 8 comments
Closed

Include GIT SHA in the binary builds #169

rshriram opened this issue Feb 17, 2017 · 8 comments

Comments

@rshriram
Copy link
Member

Sometimes, its hard to identify if Bazel built the binary or not.. It would be nice to include all the git commit versions into the binary so that we can be sure that it is using the current version.

For example, in amalgam8 we use something like this:

BUILD_SYM	:= github.com/amalgam8/amalgam8/pkg/version
LDFLAGS		+= -X $(BUILD_SYM).version=$(APP_VER)
LDFLAGS		+= -X $(BUILD_SYM).gitRevision=$(shell git rev-parse --short HEAD 2> /dev/null  || echo unknown)
LDFLAGS		+= -X $(BUILD_SYM).branch=$(shell git rev-parse --abbrev-ref HEAD 2> /dev/null  || echo unknown)
LDFLAGS		+= -X $(BUILD_SYM).buildUser=$(shell whoami || echo nobody)@$(shell hostname -f || echo builder)
LDFLAGS		+= -X $(BUILD_SYM).buildDate=$(shell date +%Y-%m-%dT%H:%M:%S%:z)
LDFLAGS		+= -X $(BUILD_SYM).goVersion=$(word 3,$(shell go version))

We need an equivalent in bazel. Could someone help with this please?

@kyessenov
Copy link
Contributor

Do you use go codegen or a bash script for this?

@rshriram
Copy link
Member Author

It's in our makefile. We use the ldflags during go build. No scripts or code gen needed.

@kyessenov
Copy link
Contributor

I see. I can write a bazel script to fill up a go source file. go tool would use the default values then.

@rshriram
Copy link
Member Author

Well we need to have a go file like the one mentioned above https://github.com/amalgam8/amalgam8/blob/master/pkg/version/version.go

@rshriram
Copy link
Member Author

Do we need to write a Go file ? Cant we make bazel include ldflags ?

go build $(BUILDFLAGS) -ldflags '$(LDFLAGS)' ...

@rshriram
Copy link
Member Author

basically we just include the same version file in our code. Populate the vars at compile time (as shown above), stick them into ldflags and compile.

@ayj
Copy link
Contributor

ayj commented Feb 22, 2017

go_binary's linkstamp attribute may be of some use here.

linkstamp String; optional; default is ""

The name of a package containing global variables set by the linker as part of a link stamp. This may be used to embed version information in the generated binary. The -X flags will be of the form -X linkstamp.KEY=VALUE. The keys and values are read from bazel-bin/volatile-status.txt and bazel-bin/stable-status.txt. If you build with --workspace_status_command=./status.sh, the output of status.sh will be written to these files. Bazel tools/buildstamp/get_workspace_status is a good template which prints Git workspace status.

@ayj
Copy link
Contributor

ayj commented Mar 8, 2017

Implemented in #265

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants