Skip to content

Instantly share code, notes, and snippets.

@civ0
Last active April 1, 2018 10:49
Show Gist options
  • Save civ0/f3d9e130852f36e0285c96bfb476b0c0 to your computer and use it in GitHub Desktop.
Save civ0/f3d9e130852f36e0285c96bfb476b0c0 to your computer and use it in GitHub Desktop.
Build a go application in a docker container
#!/bin/bash
if [ -z ${1+x} ]; then
SRC_PATH=$(pwd)
else
if [ -d "$1" ]; then
SRC_PATH=$1
else
echo "$1 is not a directory"
exit 1
fi
fi
APP_NAME=$(basename "$SRC_PATH")
echo "Building $APP_NAME..."
COMMAND="docker run --rm -v $SRC_PATH:/go/src/$APP_NAME -w /go/src/$APP_NAME golang:1.8 /bin/bash -c \"go get -v; go build -v\""
# Check if sudo is needed for docker
if groups | grep -q 'docker'; then
FIX_OWNER=false
$COMMAND
else
FIX_OWNER=true
sudo /bin/bash -c "$COMMAND"
fi
# Fix owner when scipt used sudo
if [ $FIX_OWNER == true ]
then
CHOWN_USER="$USER"
fi
# Fix owner of binary when scipt is run with sudo
if env | grep -q '^SUDO_USER='; then
CHOWN_USER=$(env | grep '^SUDO_USER=' | sed 's/^SUDO_USER=//')
fi
sudo chown -c "$CHOWN_USER:" "$SRC_PATH/$APP_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment