diff --git a/cmd/cloud_sql_proxy/build.sh b/cmd/cloud_sql_proxy/build.sh index 241251933..51aa4f9e2 100755 --- a/cmd/cloud_sql_proxy/build.sh +++ b/cmd/cloud_sql_proxy/build.sh @@ -1,4 +1,32 @@ #!/bin/bash +# +# build.sh wraps `go build` to make compiling the Cloud SQL Proxy for +# distribution more streamlined. When doing normal development on the proxy, +# `go build .` (or even `go run .`) is sufficient for iterating on the code. +# This script simply allows a convenient way to cross compile and build a docker +# container. +# +# With no arguments, this script will build a binary marked with "development", +# otherwise the binary version will be annotated with the string provided. +# +# # Build a binary labeled with 'development' +# ./build.sh +# +# # Build a binary labeled with 'beta' +# ./build.sh beta +# +# Specifying 'release' as the first argument to this script will cross compile +# for all supported operating systems and architectures. This requires a version +# identifier to be supplied as the second argument: +# +# # Build a binary for each of the supported platforms labeled with '0.001' +# ./build.sh release 0.001 +# +# Specifying docker as the first argument to this script will build a +# container, tagging it with the identifier in the second argument. +# +# # Build a docker container named 'cloud-sql-proxy:my-tag' +# ./build docker my-tag files=$(git status -s) if [[ $? != 0 ]]; then @@ -6,34 +34,78 @@ if [[ $? != 0 ]]; then exit 2 fi -RELEASE=0 -if [[ "$1" == "release" ]]; then +# build builds a new binary. Expected variables: +# VERSION: string to print out when --version is passed to the final binary +# OS: operation system to target (windows, darwin, linux, etc) +# ARCH: architecture to target (amd64, 386, etc) +# OUT: location to place binary +build() { + echo " Compile -> $OUT" + CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build \ + -ldflags "-X 'main.versionString=$VERSION'" -a -installsuffix cgo -o $OUT \ + github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy +} + +# git_version echos out version information related to the git repo and date. +git_version() { + edits="" + if [[ "$files" != "" ]]; then + edits=" (local edits)" + fi + echo "sha $(git rev-parse HEAD)$edits built $(date)" +} + +set -e + +case $1 in +"release") if [[ "$files" != "" ]]; then echo >&2 "Can't build a release version with local edits; files:" echo >&2 "$files" exit 1 fi if [[ "$2" == "" ]]; then - echo >&2 "Must provide a version number to use as a second parameter" + echo >&2 "Must provide a version number to use as the second parameter:" + echo >&2 " $0 release my-version-string" exit 1 fi - VERSION="version $2" - RELEASE=1 -else - VERSION="development" -fi - -VERSION+="; sha $(git rev-parse HEAD) built $(date)" -echo "Compiling $VERSION..." + VERSION="version $2; $(git_version)" + echo "Cross-compiling $VERSION..." -if [[ $RELEASE == 0 ]]; then - CGO_ENABLED=0 go build -ldflags "-X 'main.versionString=$VERSION'" -a -installsuffix cgo -o cloud_sql_proxy . -else for OS in windows darwin linux; do for ARCH in amd64 386; do OUT="cloud_sql_proxy.$OS.$ARCH" - echo " Compile $OUT" - CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -ldflags "-X 'main.versionString=$VERSION'" -a -installsuffix cgo -o $OUT . + build done done -fi + ;; +"docker") + if [[ "$2" == "" ]]; then + echo >&2 "Must provide a version number to use as the second parameter" + echo >&2 " $0 docker my-version-string" + exit 1 + fi + VERSION="version $2; $(git_version)" + OS="linux" + ARCH="amd64" + OUT=cloud_sql_proxy.docker + echo "Compiling $VERSION for docker..." + build + + cat >Dockerfile <