For a background on deploying a Play 1.2 application to Heroku see the last half of James Ward blog post on building a Play Web Application. Deploying a Play 2.0 application to Heroku application which connects to a PostgreSQL Database requires a number of extra steps.
In the previous tutorial we walked through creating a basic Play 2.0 Beta web application with Ebean, JSON & JQuery. Now we will deploy this web application to Heroku. First we will deploy using the default in-memory h2 database. Note that all of these commands need to be run in the root directory of playbars2.
Step 1) login to Heroku from the command line:
Step 2) Test building and running the application locally. First we need to add a plugin to the build process. In project/plugins.sbt file add:
addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.0")
When an application is deployed to Heroku, it is compiled using sbt, the scala build tool, and then run using a script specified in a Procfile. We can first test this process locally by running the sbt compiler locally:
sbt clean compile stage
If you look in the target directory you will see a start script has been generated. This is the same script Heroku will use to run the application. We can test this locally by running:
Now you can test the application by going to: http://localhost:9000/
Step 3) Add the configuration files to tell Heroku how to deploy the application. By default Heroku will try and deploy the application as a Play 1.2 application. To deploy a Play 2.0 application first we need to add a Procfile to the root directory with one line:
This is simply telling Heroku to run this web application using the script in target/start.
Step 4) Git is used to upload applications to Heroku, so we first need to setup git. When an application is with the play new command it adds a .gitignore file, so we can safely add the entire directory to git and none of the generated directories will be included.
git init git add . git commit -m 'init'
Step 5) Create a new application on Heroku and deploy the application in the cloud.
heroku create -s cedar git push heroku master
The great thing about git is you can push to multiple targets, unlike svn that has a single repository. When you run the heroku create command it has added an additional target named heroku that we push to. The heroku target is a named alias for the location of the stack that was created for us. So after setting up Heroku, I can still push to my original github repository, using the target named origin.
git push origin master
Check that your applicaiton is up using:
You can monitor the Heroku logs to see if there are any errors in the deployment using:
or to tail the logs:
heroku logs -t
Step 6) You can now test the application by going to the url specified in heroku create or running:
Step 7) Now we need to modify the application to work with the PostgreSQL Database, the database used with Heroku. First we need to add support for the PostgreSQL libraries. In the project/Build.scala add/update the following:
val postgresql = "postgresql" % "postgresql" % "9.0-801.jdbc3" val appDependencies = Seq( // Add your project dependencies here, jacksonCoreAsl, jacksonMapperAsl, postgresql )
Next we need to modify the application configuration to point to PostgreSQL. In the current Play 2.0 Beta the config files are not updated with environment variables. Instead we have to manually configure the database for the specific environment (definitely not a good practice). First find the database URL from heroku by running:
You will get something like:
DATABASE_URL => postgres://username:email@example.com/username
This needs to be translated into a proper jdbc URL. In the conf/aplication.conf comment out the in-memory database and add the url for the PostgreSQL database:
#db.default.driver=org.h2.Driver #db.default.url=jdbc:h2:mem:play db.default.url=jdbc:postgresql://ec2-107-22-193-180.compute-1.amazonaws.com/username?user=username&password=password db.default.driver=org.postgresql.Driver
Step 8) update git and Heroku with the changed files
git add project/Build.scala conf/application.conf git push heroku master
Check that your application is up using:
Now when you access the application, it will be using PostgreSQL database. Test it out by accessing: http://localhost:9000 If you need to shutdown your applications or need a clean start you can get a list of your applications with:
And then destroy the application with:
heroku apps:destroy --app sharp-ocean-4302
You now have a Play 2.0 Beta Web Application running on Heroku using a PostgreSQL database.