From b022f61f1591dd0693bbaad110f77d0503e51e38 Mon Sep 17 00:00:00 2001
From: Silas Davis <silas@monax.io>
Date: Thu, 12 Apr 2018 14:49:12 -0400
Subject: [PATCH] Get version into docker built binaries and use makefile
 recipes

Signed-off-by: Silas Davis <silas@monax.io>
---
 .dockerignore               |  2 +-
 .gitignore                  |  1 +
 Dockerfile                  | 10 +++++-----
 Makefile                    | 21 ++++++++++++++-------
 client/cmd/burrow-client.go | 12 +++++++++---
 5 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index 516f24ac..a29b1c4a 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -4,10 +4,10 @@
 .project
 run.sh
 build_tool.sh
-Makefile
 Vagrantfile
 Dockerfile
 CHANGELOG.md
 README.md
 .circleci
 docs
+bin
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index c7f40537..8d31dfaf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,4 @@ test_scratch/
 .gopath_bos
 burrow.toml
 
+commit_hash.txt
diff --git a/Dockerfile b/Dockerfile
index d50e4600..edb83bbc 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,19 +1,18 @@
 # We use a multistage build to avoid bloating our deployment image with build dependencies
-FROM golang:1.9.0-alpine3.6 as builder
+FROM golang:1.10.1-alpine3.7 as builder
 MAINTAINER Monax <support@monax.io>
 
-RUN apk add --no-cache --update git
+RUN apk add --no-cache --update git bash make
 
 ARG REPO=$GOPATH/src/github.com/hyperledger/burrow
 COPY . $REPO
 WORKDIR $REPO
 
 # Build purely static binaries
-RUN go build --ldflags '-extldflags "-static"' -o bin/burrow ./cmd/burrow
-RUN go build --ldflags '-extldflags "-static"' -o bin/burrow-client ./client/cmd/burrow-client
+RUN make build
 
 # This will be our base container image
-FROM alpine:3.6
+FROM alpine:3.7
 
 ARG REPO=/go/src/github.com/hyperledger/burrow
 
@@ -25,6 +24,7 @@ USER $USER:$USER
 
 # Copy binaries built in previous stage
 COPY --from=builder $REPO/bin/* /usr/local/bin/
+#RUN chown $USER:$USER /usr/local/bin/burrow*
 
 # Expose ports for 1337:burrow API; 46656:tendermint-peer; 46657:tendermint-rpc
 EXPOSE 1337
diff --git a/Makefile b/Makefile
index b8992062..b932a961 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ SHELL := /bin/bash
 REPO := $(shell pwd)
 GOFILES_NOVENDOR := $(shell go list -f "{{.Dir}}" ./...)
 PACKAGES_NOVENDOR := $(shell go list ./...)
-COMMIT := $(shell ./scripts/commit_hash.sh)
+LDFLAGS :=
 # Bosmarmot integration testing
 BOSMARMOT_PROJECT := github.com/monax/bosmarmot
 BOSMARMOT_GOPATH := ${REPO}/.gopath_bos
@@ -89,6 +89,11 @@ snatives:
 
 ### Building github.com/hyperledger/burrow
 
+# Output commit_hash but only if we have the git repo (e.g. not in docker build
+.PHONY: commit_hash
+commit_hash:
+	@git status &> /dev/null && scripts/commit_hash.sh > commit_hash.txt || true
+
 # build all targets in github.com/hyperledger/burrow
 .PHONY: build
 build:	check build_db build_client
@@ -99,8 +104,9 @@ build_race:	check build_race_db build_race_client
 
 # build burrow
 .PHONY: build_db
-build_db:
-	go build -ldflags "-X github.com/hyperledger/burrow/project.commit=${COMMIT}" -o ${REPO}/bin/burrow ./cmd/burrow
+build_db: commit_hash
+	go build -ldflags "-extldflags '-static' -X github.com/hyperledger/burrow/project.commit=$(shell cat commit_hash.txt)"\
+	 -o ${REPO}/bin/burrow ./cmd/burrow
 
 .PHONY: install_db
 install_db: build_db
@@ -108,8 +114,9 @@ install_db: build_db
 
 # build burrow-client
 .PHONY: build_client
-build_client:
-	go build -o ${REPO}/bin/burrow-client ./client/cmd/burrow-client
+build_client: commit_hash
+	go build -ldflags "-extldflags '-static' -X github.com/hyperledger/burrow/project.commit=$(shell cat commit_hash.txt)"\
+	 -o ${REPO}/bin/burrow-client ./client/cmd/burrow-client
 
 # build burrow with checks for race conditions
 .PHONY: build_race_db
@@ -133,8 +140,8 @@ bos: ./scripts/deps/bos.sh
 ### Build docker images for github.com/hyperledger/burrow
 
 # build docker image for burrow
-.PHONY: build_docker_db
-build_docker_db: check
+.PHONY: docker_build
+docker_build: check commit_hash
 	@scripts/build_tool.sh
 
 ### Testing github.com/hyperledger/burrow
diff --git a/client/cmd/burrow-client.go b/client/cmd/burrow-client.go
index 990f6589..bbd1d756 100644
--- a/client/cmd/burrow-client.go
+++ b/client/cmd/burrow-client.go
@@ -18,6 +18,8 @@ import (
 	"os"
 	"strconv"
 
+	"fmt"
+
 	"github.com/hyperledger/burrow/client"
 	"github.com/hyperledger/burrow/project"
 	"github.com/spf13/cobra"
@@ -35,7 +37,7 @@ Made with <3 by Monax Industries.
 
 Complete documentation is available at https://monax.io/docs
 
-VERSION: ` + project.History.CurrentVersion().String(),
+VERSION: ` + project.FullVersion(),
 	Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
 }
 
@@ -57,8 +59,12 @@ func AddGlobalFlags() {
 }
 
 func AddClientCommands() {
-	BurrowClientCmd.AddCommand(buildTransactionCommand())
-	BurrowClientCmd.AddCommand(buildStatusCommand())
+	BurrowClientCmd.AddCommand(buildTransactionCommand(), buildStatusCommand())
+	BurrowClientCmd.AddCommand(&cobra.Command{
+		Use:   "version",
+		Short: "Print full version",
+		Run:   func(cmd *cobra.Command, args []string) { fmt.Println(project.FullVersion()) },
+	})
 }
 
 //------------------------------------------------------------------------------
-- 
GitLab