Browse Source

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
Dan Duvall 2 years ago
parent
commit
d53a06a138
5 changed files with 33 additions and 1 deletions
  1. 5
    1
      Makefile
  2. 1
    0
      VERSION
  3. 8
    0
      debian/rules
  4. 6
    0
      main.go
  5. 13
    0
      meta/meta.go

+ 5
- 1
Makefile View File

@@ -3,6 +3,10 @@ PACKAGE := phabricator.wikimedia.org/source/blubber
3 3
 export BUILD_DIR=$(GOPATH)/src/$(PACKAGE)
4 4
 BINARY :=$(CURDIR)/bin/blubber
5 5
 
6
+GO_LDFLAGS := \
7
+  -X $(PACKAGE)/meta.Version=$(shell cat VERSION) \
8
+  -X $(PACKAGE)/meta.GitCommit=$(shell git rev-parse --short HEAD)
9
+
6 10
 all: clean bin/blubber
7 11
 
8 12
 clean:
@@ -13,7 +17,7 @@ bin/blubber:
13 17
 	mkdir -p $(dir $(BUILD_DIR))
14 18
 	ln -s $(CURDIR) $(BUILD_DIR)
15 19
 	cd $(BUILD_DIR) && go get ./...
16
-	cd $(BUILD_DIR) && go build -v -i
20
+	cd $(BUILD_DIR) && go build -v -i -ldflags "$(GO_LDFLAGS)"
17 21
 	mkdir -p $(CURDIR)/bin && mv $(BUILD_DIR)/blubber $(BINARY)
18 22
 
19 23
 

+ 1
- 0
VERSION View File

@@ -0,0 +1 @@
1
+0.0.1

+ 8
- 0
debian/rules View File

@@ -1,4 +1,12 @@
1 1
 #!/usr/bin/make -f
2 2
 
3
+PACKAGE := phabricator.wikimedia.org/source/blubber
4
+GO_LDFLAGS := \
5
+  -X $(PACKAGE)/meta.Version=$(shell cat VERSION) \
6
+  -X $(PACKAGE)/meta.GitCommit=$(shell git rev-parse --short HEAD)
7
+
3 8
 %:
4 9
 	dh $@ --buildsystem=golang --with=golang
10
+
11
+override_dh_auto_build:
12
+	dh_auto_build -O--buildsystem=golang -- -ldflags "$(GO_LDFLAGS)"

+ 6
- 0
main.go View File

@@ -7,9 +7,15 @@ import (
7 7
 
8 8
 	"phabricator.wikimedia.org/source/blubber/config"
9 9
 	"phabricator.wikimedia.org/source/blubber/docker"
10
+	"phabricator.wikimedia.org/source/blubber/meta"
10 11
 )
11 12
 
12 13
 func main() {
14
+	if len(os.Args) > 1 && os.Args[1] == "--version" {
15
+		fmt.Println(meta.FullVersion())
16
+		os.Exit(0)
17
+	}
18
+
13 19
 	if len(os.Args) < 3 {
14 20
 		fmt.Println("Usage: blubber config.yaml variant")
15 21
 		os.Exit(1)

+ 13
- 0
meta/meta.go View File

@@ -0,0 +1,13 @@
1
+package meta
2
+
3
+// Meta variables set dynamically by the linker at build time
4
+var (
5
+	Version   string
6
+	GitCommit string
7
+)
8
+
9
+// FullVersion returns the version string in the form of
10
+// ([major].[minor].[patch]+[commit])
11
+func FullVersion() string {
12
+	return Version + "+" + GitCommit
13
+}

Loading…
Cancel
Save