From 2f524d27c395ef0918d2410afeb853e8153e331f Mon Sep 17 00:00:00 2001 From: Rob j Loranger Date: Fri, 9 Nov 2018 10:32:55 -0800 Subject: [PATCH] add build details to binary (#866) * poc * finish JSON, remove uneeded methods, update docker file, finish script details * build.Details didn't need to be exported * typo * somehow missed this * should default to false * Fix some things remove commit SHA as build script will now create a hybrid for version as in the push-docker-images.sh script, code shamelessly stolen. change JSON function to Data and return a struct, the caller can do what they like with that data, i.e. marshall JSON for a response body. * add struct tags * use build script in docker image * newline in build.sh * use previously generated version for binary build * Working docker args removed script build.sh, was not really needed. date is generated automatically during docker build process. VERSION will be set to 'Not Specified', or the version set during our CI docker build/push step. A user can set their own version during build using ldflags. * remove old comment re: script * Yikes Almost forgot to remove my testing junk * GitHub on my phone is hard --- cmd/proxy/Dockerfile | 4 +++- cmd/proxy/main.go | 8 ++++++++ pkg/build/build.go | 37 +++++++++++++++++++++++++++++++++++ scripts/push-docker-images.sh | 2 +- 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 pkg/build/build.go diff --git a/cmd/proxy/Dockerfile b/cmd/proxy/Dockerfile index 9eeeb338..925a5643 100644 --- a/cmd/proxy/Dockerfile +++ b/cmd/proxy/Dockerfile @@ -8,7 +8,9 @@ WORKDIR $GOPATH/src/github.com/gomods/athens COPY . . -RUN GO111MODULE=on CGO_ENABLED=0 go build -mod=vendor -o /bin/athens-proxy ./cmd/proxy +ARG VERSION="Not Specified" + +RUN GO111MODULE=on CGO_ENABLED=0 go build -mod=vendor -ldflags "-X github.com/gomods/athens/pkg/build.version=$VERSION -X github.com/gomods/athens/pkg/build.buildDate=$(date -u +%Y-%m-%d-%H:%M:%S-%Z)" -o /bin/athens-proxy ./cmd/proxy FROM alpine diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 953c5186..c39cfcdc 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -2,19 +2,27 @@ package main import ( "flag" + "fmt" "log" + "os" "path/filepath" "github.com/gomods/athens/cmd/proxy/actions" + "github.com/gomods/athens/pkg/build" "github.com/gomods/athens/pkg/config" ) var ( configFile = flag.String("config_file", filepath.Join("..", "..", "config.dev.toml"), "The path to the config file") + version = flag.Bool("version", false, "Print version information and exit") ) func main() { flag.Parse() + if *version { + fmt.Println(build.String()) + os.Exit(0) + } if configFile == nil { log.Fatal("Invalid config file path provided") } diff --git a/pkg/build/build.go b/pkg/build/build.go new file mode 100644 index 00000000..81395e36 --- /dev/null +++ b/pkg/build/build.go @@ -0,0 +1,37 @@ +// Package build provides details of the built binary +// The details are set using ldflags. +// +// The ldflags can be set manually for testing locally: +// `go build -ldflags "-X github.com/gomods/athens/pkg/build.version=$(git describe --tags) -X github.com/gomods/athens/pkg/build.buildDate=$(date -u +%Y-%m-%d-%H:%M:%S-%Z)"` +package build + +import ( + "fmt" +) + +// Details represents known data for a given build +type Details struct { + Version string `json:"version,omitempty"` + Date string `json:"date,omitempty"` +} + +var version, buildDate string + +// String returns build details as a string with formatting +// suitable for console output. +// +// i.e. +// Build Details: +// Version: v0.1.0-155-g1a20f8b +// Date: 2018-11-05-14:33:14-UTC +func String() string { + return fmt.Sprintf("Build Details:\n\tVersion:\t%s\n\tDate:\t\t%s", version, buildDate) +} + +// Data returns build details as a struct +func Data() Details { + return Details{ + Version: version, + Date: buildDate, + } +} diff --git a/scripts/push-docker-images.sh b/scripts/push-docker-images.sh index f1fac209..007d734d 100755 --- a/scripts/push-docker-images.sh +++ b/scripts/push-docker-images.sh @@ -34,7 +34,7 @@ fi REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null && pwd )/" -docker build -t ${REGISTRY}proxy:${VERSION} -f ${REPO_DIR}cmd/proxy/Dockerfile ${REPO_DIR} +docker build --build-arg VERSION=${VERSION} -t ${REGISTRY}proxy:${VERSION} -f ${REPO_DIR}cmd/proxy/Dockerfile ${REPO_DIR} # Apply the mutable tag to the immutable version docker tag ${REGISTRY}proxy:${VERSION} ${REGISTRY}proxy:${MUTABLE_TAG}