What you need:
- Travis account (just log in with your GitHub credentials to create one)
- Heroku account
- Heroku CLI (
brew install heroku
) - Travis CLI (
gem install travis
)
Let's start:
- You should install Go, here's their website.
- Set your GOPATH and PATH to your
.bash_profile
etc. with:
export GOPATH=/Users/YOUR_USER_NAME/go
export PATH=$GOPATH/bin:$PATH
Where you should replace YOUR_USER_NAME with your name.
- Create a repository on GitHub.
- Go to this path and create it if it doesn't exist:
cd $GOPATH/src/github.com/GITHUB_NAME
where GITHUB_NAME is your GitHub account name and then clone the repository. This is because Go is kinda retarded how it wants you to store your projects. You should probably favourite the folder on finder by dragging the folder to the favourites. - Open the folder in your editor and create file
main.go
- Add this code to it:
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
func serveRest(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
func main() {
addr, err := determineListenAddress()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", serveRest)
log.Printf("Listening on %s...\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
panic(err)
}
}
- Install godep with
go get github.com/tools/godep
. - Init the folder with
godep save
. - Login to Heroku with
heroku login
and then initialize the Heroku app withheroku create YOUR_APP_NAME
. - Initialize Travis with
travis init
&&travis setup heroku
. You can use this as a guide:
language: go
go:
- '1.8'
deploy:
provider: heroku
api_key:
secure: mEtwIW2YPJCXIByA6BTZX3DR6N4wBBhPSxSRVXvZiSaD/VWBN3o2tyEL3nT9xtkBcybqYIM14F4GY6l0N8Vw6L8jE0T3nOtbo9uwWe4gTiQGY/XBiXe/xzEZdUNH/ri2/yUKbsSmQeiJU50rNL1nNLaeKNFA2rLH9TrL2mUeJvVp+KFYSVZdACEVXEmNzw3KaTD1L9PQ7X01UMiK+2Ci7dlIuXBb/pLS3ZaaWw+UW65M5c5zJa9dA/0jg/7B9wHZHNFcseJYV2OoVgQOq+E30YUKaCgINs7mKQkfKa/oRUNZTkXmpMnDDL4p1rCZE9A2mBgGCaAF8pjHIjkcukgacQ5G5NF4zw3J/kTMkiYf/Pf28zIzGg0ckv55vnpVjK4Dt1+77cHxyApMEg6R0F03CoXM14E4gaIKSWUQ69d/6S87BQOD9+xsfsatFY/YMU5tpFxzCKF7ED53nvkL+2cjshrDRlbjEGG43JEO/WfvRce3AdV3aKSoFqS2f6VYo+0tD8j9DuD65k7pEj2rblCKA8YvAysPaKXCbgEmY36BHSZurY4lZZMcpuq0S5wBRjdYww1Y4LMJH+JSb+wvYGRjpnpDDamHJlBSdBthfkXEBu9YLVUrAWTmUbKtghJVegyWyaIStiIsQC7c15yMEZc7s5KYyH9jcEyRLHW24Bw4AlI=
app: teemun-go-app
on:
repo: TeemuKoivisto/go-test-project
notifications:
email:
on_success: never
on_failure: never
- Create
Procfile
and add this to it:web: YOUR_REPOSITORY_NAME
so Heroku knows what to run to start the server. Go is kinda cool because it compiles the project withgo build
into binary which you can run directy with./YOUR_REPOSITORY_NAME
. - Make sure you have set Travis to build your project https://travis-ci.org/profile/.
git add
andgit commit
your code and push it to GitHub.- Watch as Travis builds the project and pushes it to Heroku.
- Profit! Go to your Heroku url to see your app in its glory.