Repository: buchgr/bazel-remote
Pull request #120 (2019-11-05 17:08:45)

log git commit earlier in the startup process
I also had to dig around a bit to figure out how to make bazel inject this value, I found the answer over in https://github.com/bazelbuild/rules_go/issues/2224


Patch:
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..ffc59c7
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1,3 @@
+# Include git version info
+build --stamp
+build --workspace_status_command 'echo STABLE_GIT_COMMIT $(git rev-parse HEAD)'
diff --git a/BUILD.bazel b/BUILD.bazel
index af9d831..43ecca1 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -34,7 +34,7 @@ go_binary(
     pure = "on",
     static = "on",
     visibility = ["//visibility:public"],
-    x_defs = {"github.com/buchgr/bazel-remote/server.GitCommit": "{STABLE_GIT_COMMIT}"},
+    x_defs = {"main.gitCommit": "{STABLE_GIT_COMMIT}"},
 )
 
 go_image(
diff --git a/linux-build.sh b/linux-build.sh
index 5e4596b..e1c9146 100755
--- a/linux-build.sh
+++ b/linux-build.sh
@@ -3,6 +3,6 @@
 set -euxo pipefail
 
 VERSION_TAG="$(git rev-parse HEAD)"
-VERSION_LINK_FLAG="github.com/buchgr/bazel-remote/server.GitCommit=${VERSION_TAG}"
+VERSION_LINK_FLAG="main.gitCommit=${VERSION_TAG}"
 
-CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-extldflags '-static' -X ${VERSION_LINK_FLAG}" .
\ No newline at end of file
+CGO_ENABLED=0 GOOS=linux go build -a -ldflags "-extldflags '-static' -X ${VERSION_LINK_FLAG}" .
diff --git a/main.go b/main.go
index 478d40d..29f7aab 100644
--- a/main.go
+++ b/main.go
@@ -31,10 +31,18 @@ const (
 	logFlags = log.Ldate | log.Ltime | log.LUTC
 )
 
+// gitCommit is the version stamp for the server. The value of this var
+// is set through linker options.
+var gitCommit string
+
 func main() {
 
 	log.SetFlags(logFlags)
 
+	if len(gitCommit) > 0 && gitCommit != "{STABLE_GIT_COMMIT}" {
+		log.Printf("bazel-remote built from git commit %s.", gitCommit)
+	}
+
 	app := cli.NewApp()
 	app.Description = "A remote build cache for Bazel."
 	app.Usage = "A remote build cache for Bazel"
@@ -221,7 +229,7 @@ func main() {
 			Handler: mux,
 		}
 		validateAC := !c.DisableHTTPACValidation
-		h := server.NewHTTPCache(proxyCache, accessLogger, errorLogger, validateAC)
+		h := server.NewHTTPCache(proxyCache, accessLogger, errorLogger, validateAC, gitCommit)
 		mux.HandleFunc("/status", h.StatusPageHandler)
 
 		cacheHandler := h.CacheHandler
diff --git a/server/http.go b/server/http.go
index 00dac8a..f9a15f5 100644
--- a/server/http.go
+++ b/server/http.go
@@ -31,6 +31,7 @@ type httpCache struct {
 	accessLogger cache.Logger
 	errorLogger  cache.Logger
 	validateAC   bool
+	gitCommit    string
 }
 
 type statusPageData struct {
@@ -41,17 +42,11 @@ type statusPageData struct {
 	GitCommit  string
 }
 
-// GitCommit is the version stamp for the server. The value of this var is set through linker options.
-var GitCommit string
-
 // NewHTTPCache returns a new instance of the cache.
 // accessLogger will print one line for each HTTP request to stdout.
 // errorLogger will print unexpected server errors. Inexistent files and malformed URLs will not
 // be reported.
-func NewHTTPCache(cache cache.Cache, accessLogger cache.Logger, errorLogger cache.Logger, validateAC bool) HTTPCache {
-	if len(GitCommit) > 0 {
-		errorLogger.Printf("Server built from git commit %s.", GitCommit)
-	}
+func NewHTTPCache(cache cache.Cache, accessLogger cache.Logger, errorLogger cache.Logger, validateAC bool, commit string) HTTPCache {
 	errorLogger.Printf("Loaded %d existing disk cache items.", cache.NumItems())
 
 	hc := &httpCache{
@@ -60,6 +55,11 @@ func NewHTTPCache(cache cache.Cache, accessLogger cache.Logger, errorLogger cach
 		errorLogger:  errorLogger,
 		validateAC:   validateAC,
 	}
+
+	if commit != "{STABLE_GIT_COMMIT}" {
+		hc.gitCommit = commit
+	}
+
 	return hc
 }
 
@@ -283,7 +283,7 @@ func (h *httpCache) StatusPageHandler(w http.ResponseWriter, r *http.Request) {
 		MaxSize:    h.cache.MaxSize(),
 		NumFiles:   h.cache.NumItems(),
 		ServerTime: time.Now().Unix(),
-		GitCommit:  GitCommit,
+		GitCommit:  h.gitCommit,
 	})
 }
 


Test patch:
diff --git a/server/http_test.go b/server/http_test.go
index 250c067..3811c61 100644
--- a/server/http_test.go
+++ b/server/http_test.go
@@ -32,7 +32,7 @@ func TestDownloadFile(t *testing.T) {
 	}
 
 	c := disk.New(cacheDir, 1024)
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 
 	req, err := http.NewRequest("GET", "/cas/"+hash, bytes.NewReader([]byte{}))
 	if err != nil {
@@ -83,7 +83,7 @@ func TestUploadFilesConcurrently(t *testing.T) {
 	}
 
 	c := disk.New(cacheDir, 1000*1024)
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 	handler := http.HandlerFunc(h.CacheHandler)
 
 	var wg sync.WaitGroup
@@ -139,7 +139,7 @@ func TestUploadSameFileConcurrently(t *testing.T) {
 	data, hash := testutils.RandomDataAndHash(1024)
 
 	c := disk.New(cacheDir, 1024)
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 	handler := http.HandlerFunc(h.CacheHandler)
 
 	var wg sync.WaitGroup
@@ -184,7 +184,7 @@ func TestUploadCorruptedFile(t *testing.T) {
 	}
 
 	c := disk.New(cacheDir, 2048)
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 	rr := httptest.NewRecorder()
 	handler := http.HandlerFunc(h.CacheHandler)
 	handler.ServeHTTP(rr, r)
@@ -225,7 +225,7 @@ func TestUploadEmptyActionResult(t *testing.T) {
 
 	c := disk.New(cacheDir, 2048)
 	validate := true
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), validate)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), validate, "")
 	rr := httptest.NewRecorder()
 	handler := http.HandlerFunc(h.CacheHandler)
 	handler.ServeHTTP(rr, r)
@@ -263,7 +263,7 @@ func TestStatusPage(t *testing.T) {
 	}
 
 	c := disk.New(cacheDir, 2048)
-	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(c, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 	rr := httptest.NewRecorder()
 	handler := http.HandlerFunc(h.StatusPageHandler)
 	handler.ServeHTTP(rr, r)
@@ -399,7 +399,7 @@ func (r fakeResponseWriter) WriteHeader(statusCode int) {
 
 func TestRemoteReturnsNotFound(t *testing.T) {
 	fake := &fakeCache{}
-	h := NewHTTPCache(fake, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true)
+	h := NewHTTPCache(fake, testutils.NewSilentLogger(), testutils.NewSilentLogger(), true, "")
 	// create a fake http.Request
 	_, hash := testutils.RandomDataAndHash(1024)
 	url, _ := url.Parse(fmt.Sprintf("http://localhost:8080/ac/%s", hash))