Simple golang build and deploy script

There are many ways to build and deploy a golang application, the best method I have found so far is to use code ship which I will cover in a future article. The simplest method is to push your code to version control on your local development environment. Then log into the production server and run a script on the server which completes the following steps:

  1. Syncs the code from version control
  2. Complies the code
  3. Stops the application via the upstart script
  4. Moves the code to the production location
  5. Starts the application via the upstart script.

If you are running a small application like even on a small server like a 512mb droplet from it should sync, compile and deploy in a matter or seconds which means little to no downtime for deploys.

The following assumptions are made in the script:

  • You have set-up a basic build environment
  • You are deploying your script to /opt/wwwroot/
  • Your upstart script is placed in /etc/init and is called sleepyfish.conf*
  • Your code is hosted in or and you have a installed git (sudo apt-get install git)

I would normally place the script in ~/ so its easy to log into the server and deploy the code without having to issue too many commands.


# Delete the old code so we get a clean pull

rm -r /usr/local/src/go/src/

# Clone your code from from the repository

git clone /usr/local/src/go/src/

# Change directory

cd /usr/local/src/go/src/

# Collect the dependencies from there repositories

go get -v

# Build the application

go build

# Stop the golang service

service sleepyfish stop

# Copy the executable into the correct location

cp /usr/local/src/go/src/ /opt/wwwroot/

# Copy the static directory containing any javascript and media

cp -R /usr/local/src/go/src/ /opt/wwwroot/

# Restart the service

service sleepyfish start

All the variables can of course be adjusted to suit your own application but it should give you a generic method to simply deploy your application. If you cant run ~/ don’t forget to set it as executable by running:

chmod 755 ~/

See also