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}