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
This commit is contained in:
Rob j Loranger
2018-11-09 10:32:55 -08:00
committed by Aaron Schlesinger
parent 0415934fd1
commit 2f524d27c3
4 changed files with 49 additions and 2 deletions
+3 -1
View File
@@ -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
+8
View File
@@ -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")
}
+37
View File
@@ -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,
}
}
+1 -1
View File
@@ -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}