Deploying Grails 3 to Heroku

This process was always a struggle for me (prior to Grails 3.)  At the time of this writing, I found that it does in deed work and if you follow the process, it will work.

However, the instructions from Heroku on Grails deploys is sadly lacking and there’s no certainty that this flow will work after this writing (things on Heroku change often it seems.)

Update build.gradle

Under the dependancies section of the build.gradle file, look for:

compile “org.springframework.boot:spring-boot-starter-tomcat”

change that to:

provided “org.springframework.boot:spring-boot-starter-tomcat”

The above needs to change because we’re going to use a Tomcat instance provided by Heroku, not what’s compiled by default in Grails 3.  Grails 3 by default will add it’s own instance of Tomcat… so we just need to mark it “provided.”

Under the dependancies (for compile) add this:

compile 'com.github.jsimone:webapp-runner:8.5.11.3'

At the root of your app you’ll have a build.gradle file, modify it and add this to bottom:

task stage() {
    dependsOn clean, war
}
war.mustRunAfter clean

task copyToLib(type: Copy) {
    into "$buildDir/server"
    from(configurations.compile) {
        include "webapp-runner*"
    }
}

stage.dependsOn(copyToLib)

Procfile

At the root of your application, add a file called “Procfile” and put the following statement in it:

web: cd build ; java -Dgrails.env=prod -jar ../build/server/webapp-runner-*.jar --expand-war --port $PORT libs/*.war

Create a Server Folder

I needed to create a server folder within the build folder.  I noticed in my failed deploys on Heroku that the logs (which you can read using “heroku logs” on the command line from your project) it said it couldn’t find the webrunner jar in the server folder.

So under /build I created a “server” folder.

Deployment

From the command line in your project root, run:

heroku login

enter your credentials

then run “heroku create

Go ahead and run a grails compile and then the following:

git add .

git commit -m “something”

git push heroku master

If all goes well, you should be successful.  When you first hit the domain that was auto created in the heroku create step, it may time out or fail.  This could be due to dyno’s still getting spun up.  Wait a few more seconds and try it in the browser again.

Logs

If you still have problems, go to the root of your project on the command line and run:

“heroku logs”

You’ll get a tail of the last 50 lines or so of the logs.  Sometimes it’s not much info, but usually it’s enough to point to your issue.