A simple go service

Setup a staging server that restarts automatically on git push
2015-09-25 golang

Goal

Create a standalone server that runs on a hosted VM that self-updates when code is deployed, for low maintenance toying around.

That’s what we call continuous deployment.

Non-Goals

This post intentionally skips:

Create a Service

  1. Create a github project that runs a web service.
  2. Make it use the excellent library github.com/facebookgo/grace/gracehttp. This way, it fork-pass-the-handle-and-exec to get 0 downtime during updates.

Run your service

For the following, I’ll use github.com/maruel/git-scan as an example since it is a simple project.

  1. Install $HOME/update.sh, see content below.
  2. We’ll use hookserve to run the webhooks as it supports the HMAC verification.
    1. go get github.com/phayes/hookserve/util/...
  3. In my case, I’m running git-scan: go get github.com/maruel/git-scan
  4. Start a screen session: screen
    1. Start the webhook: hookserve --port 7080 --secret YOUR_SECRET ./update.sh
    2. Ctrl-A, c to open a new window.
    3. Start manually your server: ./update.sh

Set up github webhook

  1. Choose long a random password, you can use apg -m 32 -a 1 for this. It doesn’t need to be memorizable.
  2. Read phayes/hookserve/README.md and setup your project. Use the url http://1.2.3.4:9000/postreceive with your IP address.

Files

That’s it!

Then you have a service that runs and that is self-updated.