Skip to content

Instantly share code, notes, and snippets.

@morhekil
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morhekil/1faf0f9c6d58368b17b1 to your computer and use it in GitHub Desktop.
Save morhekil/1faf0f9c6d58368b17b1 to your computer and use it in GitHub Desktop.
Docker container for Go builds
#!/bin/bash
BUILD_DIR=/build/go
DIST_DIR=/build/dist
PRIVATE_DIR=$BUILD_DIR/src/morhekil/example
TEST_PACKAGE=morhekil/example/main
BIN_PACKAGE=morhekil/example/cmd
source /etc/profile
# Prepare the directory structure
mkdir -p `dirname $PRIVATE_DIR`
export GOPATH=$BUILD_DIR
# Checkout private repositories if they don't exist
if [ ! -d $PRIVATE_DIR ]; then
git clone git@bitbucket.org:morhekil/example.git $PRIVATE_DIR
fi;
# Test and build
go get $BIN_PACKAGE
go get -t $TEST_PACKAGE
go test $TEST_PACKAGE && go build $BIN_PACKAGE -o $DIST_DIR/example
FROM ubuntu
# Download and unpack golang archive
RUN apt-get update
RUN apt-get install -y wget git
RUN wget https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz -O /tmp/golang.tar.gz
RUN tar -C /usr/local -xzf /tmp/golang.tar.gz
# Configure the environment
RUN echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile.d/go.sh
RUN echo 'export GOPATH=$HOME/go' >> /etc/profile.d/go.sh
# Set up ssh client
RUN echo "IdentityFile ~/.ssh/id_rsa" >> /etc/ssh/ssh_config
RUN mkdir -p /root/.ssh
COPY id_rsa /root/.ssh/id_rsa
RUN chown 600 /root/.ssh/id_rsa
RUN ssh-keyscan -H bitbucket.org >> /root/.ssh/known_hosts
RUN ssh-keyscan -H github.com >> /root/.ssh/known_hosts
# Copy the buildscript
COPY build.sh /usr/local/bin/build
@morhekil
Copy link
Author

First rough version of Docker container for Go builds. Checks out the sources, runs tests and builds the binary.

Assuming the image has been built with "gobuilder" tag (e.g. docker build -t gobuilder .), and there's a dist subdirectory on the current path, usage is:

docker run --rm -v `pwd`/dist:/build/dist/ gobuilder build

It will save the compiled binary into dist for further use.

Next up: automated deployment (capistrano?). Blog post is coming up right after that at speakmy.name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment