Skip to content

Instantly share code, notes, and snippets.

@swnim
Created August 8, 2018 01:58
Show Gist options
  • Save swnim/72bfd3f75d146a13203cbce01c76989a to your computer and use it in GitHub Desktop.
Save swnim/72bfd3f75d146a13203cbce01c76989a to your computer and use it in GitHub Desktop.
AWS EB Golang Build Script
#!/bin/bash -xe
GOPATH="/tmp/go"
APP_BUILD_DIR="$GOPATH/src/project" # We will build the app here
APP_STAGING_DIR=$(pwd) # Current directory
DEP_VERSION="v0.5.0" # Use specific version for stability
# Install dep, a Go dependency management tool, if not already installed or if
# the version does not match.
if ! hash dep 2> /dev/null || [[ $(dep version | awk 'NR==2{print $3}') != "$DEP_VERSION" ]]; then
curl -L -s https://github.com/golang/dep/releases/download/$DEP_VERSION/dep-linux-amd64 -o /usr/local/bin/dep
chmod +x /usr/local/bin/dep
fi
# Remove the $APP_BUILD_DIR just in case it was left behind in a failed build.
rm -rf $APP_BUILD_DIR
# Setup the application directory
mkdir -p $APP_BUILD_DIR
# mv all files to $APP_BUILD_DIR
# https://superuser.com/questions/62141/how-to-move-all-files-from-current-directory-to-upper-directory
mv ./* .[^.]* $APP_BUILD_DIR
cd $APP_BUILD_DIR
dep ensure
# Build the binary with jsoniter tag.
go build -a -o application
# Modify permissons to make the binary executable.
chmod +x application
# Move the binary back to staging dir.
mkdir "$APP_STAGING_DIR/bin"
# By default, `bin/application` is executed. This way, a Procfile isn't needed.
mv application "$APP_STAGING_DIR/bin"
# Clean up.
rm -rf $APP_BUILD_DIR
echo "Build successful!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment