From 4d094dc180e78f6d5df98ea48c58d384481eb8c7 Mon Sep 17 00:00:00 2001 From: Kevin Malachowski Date: Tue, 19 Apr 2016 16:31:19 -0700 Subject: [PATCH 1/3] Add support for building a docker container in build.sh --- cmd/cloud_sql_proxy/build.sh | 92 +++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 17 deletions(-) diff --git a/cmd/cloud_sql_proxy/build.sh b/cmd/cloud_sql_proxy/build.sh index 241251933..f43c67403 100755 --- a/cmd/cloud_sql_proxy/build.sh +++ b/cmd/cloud_sql_proxy/build.sh @@ -1,4 +1,20 @@ #!/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" +# +# 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. +# +# Specifying 'docker' as the first argument to this script will build a +# container, tagging it with the second argument. The tag will also be used as +# the version identifier. files=$(git status -s) if [[ $? != 0 ]]; then @@ -6,34 +22,76 @@ 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" 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" + 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 < Date: Wed, 27 Apr 2016 12:32:57 -0700 Subject: [PATCH 2/3] Changes from code review. Add some example commands and gave examples when the command line was incorrect. Also make the docker container be named 'cloud-sql-proxy' and be tagged with whatever was provided on the command line. --- cmd/cloud_sql_proxy/build.sh | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/cmd/cloud_sql_proxy/build.sh b/cmd/cloud_sql_proxy/build.sh index f43c67403..e9badd7a3 100755 --- a/cmd/cloud_sql_proxy/build.sh +++ b/cmd/cloud_sql_proxy/build.sh @@ -6,15 +6,27 @@ # 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" +# 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. +# 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 second argument. The tag will also be used as -# the version identifier. +# 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 @@ -50,10 +62,11 @@ case $1 in if [[ "$files" != "" ]]; then echo >&2 "Can't build a release version with local edits; files:" echo >&2 "$files" - exit 1 + #exit 1 fi if [[ "$2" == "" ]]; then - echo >&2 "Must provide a version number to use as the 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; $(git_version)" @@ -69,6 +82,7 @@ case $1 in "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)" @@ -84,7 +98,7 @@ FROM scratch COPY cloud_sql_proxy.docker /cloud_sql_proxy EOF echo "Building docker container (tag: $2)..." - docker build -t "$2" . + docker build -t "cloud-sql-proxy:$2" . # Cleanup rm Dockerfile cloud_sql_proxy.docker From f4c2fc81cc6e63b285575960628165f6734930ee Mon Sep 17 00:00:00 2001 From: Kevin Malachowski Date: Wed, 27 Apr 2016 12:34:31 -0700 Subject: [PATCH 3/3] Correctly exit when there are local edits --- cmd/cloud_sql_proxy/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cloud_sql_proxy/build.sh b/cmd/cloud_sql_proxy/build.sh index e9badd7a3..51aa4f9e2 100755 --- a/cmd/cloud_sql_proxy/build.sh +++ b/cmd/cloud_sql_proxy/build.sh @@ -62,7 +62,7 @@ case $1 in if [[ "$files" != "" ]]; then echo >&2 "Can't build a release version with local edits; files:" echo >&2 "$files" - #exit 1 + exit 1 fi if [[ "$2" == "" ]]; then echo >&2 "Must provide a version number to use as the second parameter:"