Documentation

You are viewing the documentation for the 2.4.0-RC3 development release. The latest stable release series is 3.0.x.

§Deploying to Heroku

Heroku is a cloud application platform – a way of building and deploying web apps.

To get started:

  1. Install the Heroku Toolbelt
  2. Sign up for a Heroku account

There are two methods of deployment to Heroku:

§Deploying to a remote Git repository

§Store your application in git

$ git init
$ git add .
$ git commit -m "init"

§Create a new application on Heroku

$ heroku create
Creating warm-frost-1289... done, stack is cedar-14
http://warm-frost-1289.herokuapp.com/ | [email protected]:warm-frost-1289.git
Git remote heroku added

This provisions a new application with an HTTP (and HTTPS) endpoint and Git endpoint for your application. The Git endpoint is set as a new remote named heroku in your Git repository’s configuration.

§Deploy your application

To deploy your application on Heroku, use Git to push it into the heroku remote repository:

$ git push heroku master
Counting objects: 93, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (84/84), done.
Writing objects: 100% (93/93), 1017.92 KiB | 0 bytes/s, done.
Total 93 (delta 38), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Play 2.x - Scala app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Downloading SBT... done
remote: -----> Priming Ivy cache (Scala-2.11, Play-2.3)... done
remote: -----> Running: sbt update
...
remote: -----> Dropping ivy cache from the slug
remote: -----> Dropping sbt boot dir from the slug
remote: -----> Dropping compilation artifacts from the slug
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 93.3MB
remote: -----> Launching... done, v6
remote:        https://warm-frost-1289.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/warm-frost-1289.git
* [new branch]      master -> master

Heroku will run sbt clean stage to prepare your application. On the first deployment, all dependencies will be downloaded, which takes a while to complete (but will be cached for future deployments).

§Check that your application has been deployed

Now, let’s check the state of the application’s processes:

$ heroku ps
=== web (1X): `target/universal/stage/bin/sample-app -Dhttp.port=${PORT}`
web.1: up 2015/01/09 11:27:51 (~ 4m ago)

The web process is up. Review the logs for more information:

$ heroku logs
2011-08-18T00:13:41+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/myapp`
2011-08-18T00:14:18+00:00 app[web.1]: Starting on port:28328
2011-08-18T00:14:18+00:00 app[web.1]: Started.
2011-08-18T00:14:19+00:00 heroku[web.1]: State changed from starting to up
...

We can also tail the logs in the same manner as we could do at a regular command line. This is useful for debugging:

$ heroku logs -t --app warm-frost-1289
2011-08-18T00:13:41+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/myapp`
2011-08-18T00:14:18+00:00 app[web.1]: Starting on port:28328
2011-08-18T00:14:18+00:00 app[web.1]: Started.
2011-08-18T00:14:19+00:00 heroku[web.1]: State changed from starting to up
...

Looks good. We can now visit the app by running:

$ heroku open

§Deploying with the sbt-heroku plugin

The Heroku sbt plugin utilizes an API to provide direct deployment of prepackaged standalone web applications to Heroku. This may be a preferred approach for applications that take a long time to compile, or that need to be deployed from a Continuous Integration server such as Travis CI or Jenkins.

§Adding the plugin

To include the plugin in your project, add the following to your project/plugins.sbt file:

resolvers += Resolver.url("heroku-sbt-plugin-releases", url("https://dl.bintray.com/heroku/sbt-plugins/"))(Resolver.ivyStylePatterns)

addSbtPlugin("com.heroku" % "sbt-heroku" % "0.3.0")

Next, we must configure the name of the Heroku application the plugin will deploy to. But first, create a new app. Install the Heroku Toolbelt and run the create command with the -n flag, which will prevent it from adding a Git remote.

$ heroku create -n
Creating obscure-sierra-7788... done, stack is cedar-14
http://obscure-sierra-7788.herokuapp.com/ | [email protected]:obscure-sierra-7788.git

Now add something like this to your build.sbt, but replace “obscure-sierra-7788” with the name of the application you created.

herokuAppName in Compile := "obscure-sierra-7788"

The sbt-heroku project’s documentation contains details on configuring the execution of the plugin.

§Deploying with the plugin

With the plugin added, you can deploy to Heroku by running this command:

$ sbt stage deployHeroku
...
[info] ---> Packaging application...
[info]      - including: ./target/universal/stage
[info] ---> Creating slug...
[info]      - file: ./target/heroku/slug.tgz
[info]      - size: 63MB
[info] ---> Uploading Slug...
[info]      - id: 73c1f7f2-75a4-4bb9-a3ce-e7ec2d70fa96
[info]      - stack: cedar-14
[info]      - process types: web
[info] ---> Releasing...
[info]      - version: 65
[success] Total time: 90 s, completed Aug 29, 2014 3:36:43 PM

And you can visit your application by running this command:

$ heroku open -a obscure-sierra-7788

You can see the logs for you application by running this command:

$ heroku logs -a obscure-sierra-7788

§Connecting to a database

Heroku provides a number of relational and NoSQL databases through Heroku Add-ons. Play applications on Heroku are automatically provisioned a Heroku Postgres database. To configure your Play application to use the Heroku Postgres database, first add the PostgreSQL JDBC driver to your application dependencies (build.sbt):

libraryDependencies += "postgresql" % "postgresql" % "9.1-901-1.jdbc4"

Then create a new file in your project’s root directory named Procfile (with a capital “P”) that contains the following (substituting the myapp with your project’s name):

web: target/universal/stage/bin/myapp -Dhttp.port=${PORT} -DapplyEvolutions.default=true -Ddb.default.driver=org.postgresql.Driver -Ddb.default.url=${DATABASE_URL}

This instructs Heroku that for the process named web it will run Play and override the applyEvolutions.default, db.default.driver, and db.default.url configuration parameters. Note that the Procfile command can be maximum 255 characters long. Alternatively, use the -Dconfig.resource= or -Dconfig.file= mentioned in production configuration page.
Note that the creation of a Procfile is not actually required by Heroku, as Heroku will look in your play application’s conf directory for an application.conf file in order to determine that it is a play application.

§Further learning resources

Next: Deploying to Cloud Foundry