I see many Go projects on GitHub that mention publicly releasing binaries for their projects in the future when time permits. After releasing Depcon and Docker Volume Netshare I wanted an easy way to release updates so users could quickly grab the latest binary regardless of their platform.
There are many ways to do this and after researching various cross-compilation options and a place to store built binaries I settled on Goxc for compilation and Bintray to store the final binaries and packages.
Here’s how to quickly setup your Golang project to build and easily deploy binaries.
1. Setting up Bintray
Bintray gives allows developers to store, publish, download, promote and distribute software with advanced features that fully automate the software distribution process.
- If you don’t have an account already you can sign up for free here.
- Once you have signed in or created an account you will want to create a
generic repository
that categories your project. For example if the project it orientated towards Docker you could call your repository “docker”. - Click into your newly created or existing repository and
create a new package
. This should ideally be the project name of where you’ll be publishing your binaries too. - Now that you have a repository and package for your project you’ll want to copy your API Key for the steps below. This can be done by editing your profile and selecting the vertical tab called “API Key” as shown below.
Exporting the API Key:
The steps below will require the API Key to successfully authenticate with Bintray upon a release of new artifacts. Copy the API Key from your bintray into your clipboard.
So the the API Key is available in the step below run the following in your current terminal:
- Execute:
export BINTRAY_APIKEY="MYAPI_KEY" >> ~/.bash_profile
- Execute:
source ~/.bash_profile
2. Setting up Goxc
Installing Goxc is quite easy. This guide assumes you already have a Go environment setup. The following will add the Goxc executable to your $GOPATH.
go get github.com/laher/goxc
Next we want to install the toolchains for all platforms. This adds support to compile against different operating systems with Goxc. This can take a few minutes to complete.
goxc -t
3. Creating the .goxc.json build file
The .goxc.json
file sets up metadata for tasks within Goxc. The example below is a simplified artifact that can be tailored to quickly deploy cross platform binaries to bintray. For more advanced examples refer to the .goxc.json file within docker-volume-netshare which also adds sysinit scripts for debian based packages.
{ "ArtifactsDest": "build", "ConfigVersion": "0.9", "PackageVersion": "0.1", "TaskSettings": { "bintray": { "user": "bintray-username", "package": "my-project", "repository": "my-repo" } }
Explanation of the example above:
- PackageVersion: the version of the binary to be released
- bintray -> user: your bintray username
- bintray -> package: the bintray package name create above to deploy the artifacts under
- bintray -> repository: the bintray repository name
4. Creating a Makefile
The Makefile will be used to release your binaries. I personally add extra targets to automatically build and format the Go source code and an additional call to release the binaries to bintray.
GO_FMT = gofmt -s -w -l . GO_XC = goxc -os="linux freebsd openbsd netbsd" GOXC_FILE = .goxc.local.json all: deps compile compile: goxc goxc: $(shell echo '{\n "ConfigVersion": "0.9",' > $(GOXC_FILE)) $(shell echo ' "TaskSettings": {' >> $(GOXC_FILE)) $(shell echo ' "bintray": {\n "apikey": "$(BINTRAY_APIKEY)"' >> $(GOXC_FILE)) $(shell echo ' }\n } \n}' >> $(GOXC_FILE)) $(GO_XC) deps: go get format: $(GO_FMT) bintray: $(GO_XC) bintray
5. Putting it all together
Now that you have setup Bintray, Goxc and created a Makefile you can simply run the repeatable steps below to format & compile and release your newest binaries to Bintray.
Formatting and Building: make
Uploading to Bintray: make bintray
Thats it! I hope this guide has helped you quickly evolve your current Golang project into a releasable binary project. Since the $BINTRAY_APIKEY is a local environment no confidential information is exposed and all changes can be checked in to your local repo without exposing private information.