Easy-peasy github releases for go projects using travis


A general theme of this blog is making life easier (or programmer laziness), so continuing in this vane this is a quick post on automated releases of go executables onto your github releases page.

One of the strengths of go (which just got even easier with 1.5) is the ability to cross-compile self-contained executables - I find this particularly handy to get command line tools onto remote servers (though as with fine arts, wines, etc always be sure you trust the provenance of random executables you download…).

I’ll be using travis to build the github project, and github to host the release binaries. I assume you’ve a go project on github with some code ready to be built.

(Replace with ‘user’ with your username and ‘project’ with your github project name in all the steps below.)

First…

Create a travis project and link it to this github repo.

Now setup your cross-build. I usually manage travis projects with a simple Makefile in the project root:

package = github.com/user/project

.PHONY: release

release:
	mkdir -p release
	GOOS=linux GOARCH=amd64 go build -o release/myproject-linux-amd64 $(package)
	GOOS=linux GOARCH=386 go build -o release/myproject-linux-386 $(package)
	GOOS=linux GOARCH=arm go build -o release/myproject-linux-arm $(package)
	...and so on...

Note: You need the .PHONY line, otherwise make will assume the ‘release’ means it’s already done after the first run.

You’ll also want to ignore the release directory - add it to gitignore:

$ echo release >> .gitignore

Now create a minimal .travis.yml in the root of your project:

language: go
go:
- 1.5

You’ll also need the travis command line tool - it’s a ruby tool, so go off and install that. It’s ok, I’ll wait.

Now run:

$ travis setup releases

This will prompt for your github username, password. For ‘File to Upload’, enter one of the release files from above ‘release/myproject-linux-amd64’ – we’ll add the rest in a sec. Answer ‘yes’ to “Deploy only from user/myproject” and “Encrypt API key”:

$ travis setup releases
Detected repository as user/project, is this correct? |yes| 
Username: user
Password for user: **********
Two-factor authentication code for user: ...
File to Upload: release/myproject-linux-amd64
Deploy only from user/project? |yes| 
Encrypt API key? |yes| 

This adds the following to your .travis.yml:

deploy:
  provider: releases
  api_key:
    secure: ...
  file: release/myproject-linux-amd64
  on:
    repo: user/project

Adjust this by adding the other release files, and also set the release to only run on tags:

deploy:
  provider: releases
  api_key:
    secure: ...
  file:
  - release/myproject-linux-amd64
  - release/myproject-linux-386
  - release/myproject-linux-arm
  on:
    repo: user/project
    tags: true

Git add .travis.yml and .gitignore and commit the change.

$ git add .travis.yml .gitignore
$ git commit -m 'Making travis work'

Finally, tag this as a release, and push to github:

$ git tag -a 1.0.0 -m 'Release 1.0.0'
$ git push --tags origin master

Watch your travis page, and all going to plan this should build and push the release binaries to your github release page (https://github.com/user/project/releases), ready for download.