summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Duvall <dduvall@wikimedia.org>2017-10-16 09:47:43 -0700
committerDan Duvall <dduvall@wikimedia.org>2017-10-18 14:46:15 -0700
commitd53a06a138106c1652fff0b8576ae907a29a727c (patch)
tree4e9ec388de0effbd1752a030d34aec0737df3f3c
parent01f3533a0b4819ce868d4f89c83a090eae6cfd08 (diff)
downloadblubber-d53a06a138106c1652fff0b8576ae907a29a727c.tar.gz
Capture and expose build-time meta data
Summary: The `go build` tool can accept linker options that dynamically set variable values at build time. Let's make use of that in our `Makefile` and `debian/rules` to know and expose meta data such as version and Git commit at runtime. Test Plan: Run `make bin/blubber && bin/blubber --version` and verify that it outputs "0.0.1-[git head commit]". Build and install the debian package and verify the same using the installed binary. Reviewers: thcipriani, hashar, Joe, #release-engineering-team Reviewed By: thcipriani, #release-engineering-team Tags: #release-engineering-team Differential Revision: https://phabricator.wikimedia.org/D816
-rw-r--r--Makefile6
-rw-r--r--VERSION1
-rwxr-xr-xdebian/rules8
-rw-r--r--main.go6
-rw-r--r--meta/meta.go13
5 files changed, 33 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index bfd7ab8..116e2b9 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,10 @@ PACKAGE := phabricator.wikimedia.org/source/blubber
export BUILD_DIR=$(GOPATH)/src/$(PACKAGE)
BINARY :=$(CURDIR)/bin/blubber
+GO_LDFLAGS := \
+ -X $(PACKAGE)/meta.Version=$(shell cat VERSION) \
+ -X $(PACKAGE)/meta.GitCommit=$(shell git rev-parse --short HEAD)
+
all: clean bin/blubber
clean:
@@ -13,7 +17,7 @@ bin/blubber:
mkdir -p $(dir $(BUILD_DIR))
ln -s $(CURDIR) $(BUILD_DIR)
cd $(BUILD_DIR) && go get ./...
- cd $(BUILD_DIR) && go build -v -i
+ cd $(BUILD_DIR) && go build -v -i -ldflags "$(GO_LDFLAGS)"
mkdir -p $(CURDIR)/bin && mv $(BUILD_DIR)/blubber $(BINARY)
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..8acdd82
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.0.1
diff --git a/debian/rules b/debian/rules
index 8cce5e0..1abf6fb 100755
--- a/debian/rules
+++ b/debian/rules
@@ -1,4 +1,12 @@
#!/usr/bin/make -f
+PACKAGE := phabricator.wikimedia.org/source/blubber
+GO_LDFLAGS := \
+ -X $(PACKAGE)/meta.Version=$(shell cat VERSION) \
+ -X $(PACKAGE)/meta.GitCommit=$(shell git rev-parse --short HEAD)
+
%:
dh $@ --buildsystem=golang --with=golang
+
+override_dh_auto_build:
+ dh_auto_build -O--buildsystem=golang -- -ldflags "$(GO_LDFLAGS)"
diff --git a/main.go b/main.go
index 5273668..a684d11 100644
--- a/main.go
+++ b/main.go
@@ -7,9 +7,15 @@ import (
"phabricator.wikimedia.org/source/blubber/config"
"phabricator.wikimedia.org/source/blubber/docker"
+ "phabricator.wikimedia.org/source/blubber/meta"
)
func main() {
+ if len(os.Args) > 1 && os.Args[1] == "--version" {
+ fmt.Println(meta.FullVersion())
+ os.Exit(0)
+ }
+
if len(os.Args) < 3 {
fmt.Println("Usage: blubber config.yaml variant")
os.Exit(1)
diff --git a/meta/meta.go b/meta/meta.go
new file mode 100644
index 0000000..a5ee581
--- /dev/null
+++ b/meta/meta.go
@@ -0,0 +1,13 @@
+package meta
+
+// Meta variables set dynamically by the linker at build time
+var (
+ Version string
+ GitCommit string
+)
+
+// FullVersion returns the version string in the form of
+// ([major].[minor].[patch]+[commit])
+func FullVersion() string {
+ return Version + "+" + GitCommit
+}