自动化的Go App部署

I'm wondering if there are any convenient ways to automate deployment of code to a live server in GO, either standard built-in methods, or otherwise.

I want something google app engine like, I just run the command and it uploads to the server and triggers a restart.

(Ultimately I want a git commit to trigger a rebuild and redeploy, but thats for down the track in the future)

I recommend Travis CI + Heroku.

You can deploy to heroku directly with just a git push, but I like to use Travis to build and run the tests before that.

There are some guides online but I'll try to go directly to the point:

What you will need?

  • Github account
  • Travis account (linked with github, free if open source)
  • Empty Heroku app (Free dyno works great)

Setup

In your github repo, create the following files:

After that go to your Travis account, add your repository and enabled the build for it.

Here is a sample minimal config file content (based on my app that I deploy to heroku):

.travis.yml

language: go
go:
  - tip
deploy:
  provider: heroku
  buildpack: https://github.com/kr/heroku-buildpack-go.git
  api_key:
    secure: <your heroku api key encripted with travis encrypt>
  on: master 

Procfile

worker: your-app-binary

.go-dir

your-app-binary

Procfile and .go-dir are heroku configs so it can vary if you are deploying a web app, you can read more at the heroku documentation

One important and easily missed point is the build pack, without it the deploy will not work.

Read the Travis docs to see how to encrypt the heroku key

How it works?

Basically, every push to your repository will trigger the Travis CI build, if it passes it will deploy the app to heroku, so you set this up once and build + deploy is just a push away ;)

Also Travis will build and updated the status of all Pull Requests to your repository automagically.

To see my config and build, please take a look at my Travis build and my repository with my working configs