A simple go service
Setup a staging server that restarts automatically on git push
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:
- Continuous integration testing, which is a separate subject. We assume you
run tests before accepting commits.
- You may want to run off a release branch. That’s a good idea.
- Monitoring, log analysis, especially for security.
- Load balancing. Services like GCE and EC2 have integrated load balancer so
no need to run nginx locally.
- No container deployment, as this requires a CI to generate the Docker file.
Create a Service
- Create a github project that runs a web service.
- 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.
- Install
$HOME/update.sh
, see content below.
- We’ll use hookserve to run the
webhooks as it supports the HMAC verification.
go get github.com/phayes/hookserve/util/...
- In my case, I’m running git-scan:
go get github.com/maruel/git-scan
- Start a screen session:
screen
- Start the webhook:
hookserve --port 7080 --secret YOUR_SECRET ./update.sh
- Ctrl-A, c to open a new window.
- Start manually your server:
./update.sh
Set up github webhook
- Choose long a random password, you can use
apg -m 32 -a 1
for this. It
doesn’t need to be memorizable.
- 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.