Offline
Online
Viewers

Deploying Go Revel Apps On Heroku

I’ve spent quite a bit of time trying to figure out how to successfully deploy Go Revel applications to Heroku, luckily for you.  Let’s go through them step by step and I’ll show you how I did it.

Install Go

First install Go if you haven’t already, this should be pretty self explanatory.

Install Revel

Next, install the Revel framework.  Change to the directory where you have your GOPATH configured, for example:

user@computer:$ cd $GOPATH

Then execute the following to install Revel:

user@computer:$ go get github.com/revel/revel
user@computer:$ go get github.com/revel/cmd/revel

Update your path to contain the Revel binary:

user@computer:$ export PATH=$PATH:$GOPATH/bin

Now change directory to the src directory:

user@computer:$ cd $GOPATH/src

Create the app

If you haven’t already done so, create a basic vanilla app using revel by executing the following command (replaceing “myapp” with your application name of course):

user@computer:$ revel new myapp
user@computer:$ cd myapp

Next, let’s make sure the app runs by executing the run command:

user@computer:$ revel run myapp

Open a browser to http://localhost:9000 (or whatever port you have your application configured to run on) and verify that everything is working as expected.

Install Heroku Toolbelt

Download and install the Heroku Toolbelt: https://toolbelt.heroku.com/

Next login to Heroku using the toolbelt:

user@computer:$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password (typing will be hidden):
Authentication successful.

Once that’s done you’ll need to create the application.  If you’ve already done so by way of the Heroku web GUI, you can skip this step, if not execute the following (Once again replacing “myapp” with your actual app name):

user@computer:$ heroku create -b https://github.com/revel/heroku-buildpack-go-revel.git myapp

This will ensure that the revel go buildpack is being used.

If you’ve already created the application using the Heroku web GUI, then go under settings and set the buildpack to use “https://github.com/revel/heroku-buildpack-go-revel.git”

heroku-buildpack

Initialize Git

Next ensure that you are still under your application directory “src/myapp” and execute the following:

user@computer:$ git init

Now that your git is initialized let’s link it to the Heroku online app repo, execute the following (Replace myapp with your actual app name).

user@computer:$ heroku git:remote -a myapp

Install Godep

Now, we need to install Godep for our project to work correctly on Heroku, install by executing the following:

user@computer:$ go get github.com/tools/godep

Next, let’s automatically update all of our dependencies using godep, execute the following under your application directory:

user@computer:$ godep save ./…

If this ran correctly, you should see a “Godeps” and “vendor” folder under your application directory. Note: pay special attention to the “./…” that needs to be there verbatim.

 

Test Heroku Local

To ensure everything is going to work in the Heroku environment, we should first test the application locally by executing the following:

user@computer:$ heroku local

[WARN] ENOENT: no such file or directory, open ‘Procfile’
[FAIL] No Procfile and no package.json file found in Current Directory – See heroku local –help

You probably noticed the errors there, this isn’t really an issue, we just need to complete a few things.  First lets create a file called “.env” and add an environment variable to it that tells Heroku to use a version later than go 1.5, execute the following:

user@computer:$ echo “GO15VENDOREXPERIMENT=1” > .env

This file should be located at the root of your application directory (“myapp/.env”).

Next, let’s create a simple Procfile to execute our revel run command.  Execute the following:

user@computer:$ echo “web:revel run myapp prod 9000” > Procfile

Finally, let’s add our application name to the “.godir” file.  Execute the following:

user@computer:$ echo “myapp” > .godir

Now, let’s run “heroku local” again, you should see something similar to the following:

user@computer:$ heroku local

[OKAY] Loaded ENV .env File as KEY=VALUE Format
3:37:24 PM web.1 | ~
3:37:24 PM web.1 | ~ revel! http://revel.github.io
3:37:24 PM web.1 | ~
3:37:24 PM web.1 |
3:37:24 PM web.1 | INFO 2016/06/29 15:37:24 revel.go:365: Loaded module static
3:37:24 PM web.1 | INFO 2016/06/29 15:37:24 revel.go:365: Loaded module testrunner
3:37:24 PM web.1 | INFO 2016/06/29 15:37:24 revel.go:230: Initialized Revel v0.13.1 (2016-06-06) for >= go1.4
3:37:24 PM web.1 | INFO 2016/06/29 15:37:24 run.go:57: Running myapp (myapp) in dev mode
3:37:24 PM web.1 | INFO 2016/06/29 15:37:24 harness.go:170: Listening on :9000
3:37:24 PM web.1 |

Now open your browser to http://localhost:9000 again and verify that it runs correctly in the heroku environment.

If everything runs well locally, then we’re ready to deploy.

Deploy To Heroku

First let’s add all of our changes to git, execute the following:

user@computer:$ git add .

Next, let’s commit:

user@computer:$ git commit -m “Initial commit”

Now let’s push the changes to Heroku

user@computer:$ git push heroku master

Counting objects: 377, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (352/352), done.
Writing objects: 100% (377/377), 845.31 KiB | 0 bytes/s, done.
Total 377 (delta 96), reused 0 (delta 0)
remote: Compressing source files… done.
remote: Building source:
remote:
remote: —–> Fetching set buildpack https://github.com/revel/heroku-buildpack-go-revel.git… done
remote: —–> Revel app detected
remote: —–> Installing go1.6… done
remote: Installing Virtualenv… done
remote: Installing Mercurial… done
remote: Installing Bazaar… done
remote: —–> Running: godep restore
remote:
remote: —–> Discovering process types
remote: Default types for buildpack -> web
remote:
remote: —–> Compressing…
remote: Done: 127.8M
remote: —–> Launching…
remote: Released v4
remote: https://obscure-beyond-93544.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy… done.
To https://git.heroku.com/obscure-beyond-93544.git

Once completed you should be ready to go.  Next run the following to test:

user@computer:$ heroku open

If you received errors during deployment or testing, you may be able to help resolve your issue by viewing the heroku logs:

user@computer:$ heroku logs

 

Hopfully everything turned out well for you.  If it didn’t, drop me a line and I’ll see if I can help you out.

2 Comments

  1. I got below error. Would you please help me? Mentioning that i am using go version 1.8.3 but in the heroku it is showing go version 1.4. It really confused me.

    remote:
    remote: —–> Revel app detected
    remote: —–> Installing go1.4… done
    remote: Installing Virtualenv… done
    remote: Installing Mercurial… done
    remote: Installing Bazaar… done
    remote: —–> Running: go get -tags heroku ./…
    remote: package github.com/revel/revel
    remote: imports github.com/agtorre/gocolorize
    remote: imports github.com/klauspost/compress/gzip
    remote: imports github.com/klauspost/cpuid
    remote: imports github.com/klauspost/compress/zlib
    remote: imports github.com/revel/config
    remote: imports github.com/revel/pathtree
    remote: imports golang.org/x/net/websocket
    remote: imports gopkg.in/fsnotify.v1
    remote: imports golang.org/x/sys/unix
    remote: imports github.com/go-gorp/gorp
    remote: imports context: unrecognized import path “context”
    remote: ! Push rejected, failed to compile Revel app.
    remote:
    remote: ! Push failed
    remote: Verifying deploy…

    1. You may be using a version that is newer than the current one supported by Heroku. You may want to try downgrading your local version to match Heroku’s.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Affiliates