About the author

My photo

Java, Scala, and everything

Wednesday, November 30, 2011

Play! with Heroku

Recently, inspired by some talks on Devoxx, I decided to check Play framework and Heroku.

Play is another java web framework, but this one is heavily influenced by Ruby on Rails. Convention over configuration, little code necessary etc. Looks like Play is gaining momentum, version 2 (now beta) supports Scala and now it became part of Typesafe stack.

Heroku is another cloud. Well, quite different than AWS or GAE (actually, as far as I know, it works on AWS). Heroku is not only running applications, it is also able to build and deploy them. So to deploy your changes, it's enough to make git push.

So first, download Play 2 beta from this site. Unpack, add dir to your $PATH, so that you could use play command from anywhere.

Go to dir where you want to create the app and issue play new helloapp (where helloapp is your app name). Play should create helloapp directory. No go into it. You can notice that Play nicely added .gitignore file there, so when you create git repository here, unnecessary files won't be added. Play created fully functional, basic web application. You can issue play run in helloapp directory and the server is going to be started and application deployed. You can now see it on localhost:9000.

Now you'll need account on Heroku. Create one on heroku.com, it's easy as creating email or forum account. Then just activate it by email sent to you from Heroku.

For your app to be able to run on Heroku cloud, you'll need to add one more file. In the helloapp dir create Procfile file with content like this:
web: target/start
If you created your app with Play 1.x instead of 2.0, Procfile should be quite different:
web: play run --http.port=$PORT $PLAY_OPTS

To interact with the cloud you'll also need heroku-toolbelt. Get one from here.

Ok, you have your app and all the tools necessary to deploy it to the cloud. You'll also need git, which I assume you have already installed ;) As I mentioned on the beginning, it's necessary to push your app's code to Heroku.

Create git repository in the helloapp directory. Enter the directory and issue the following comands.
git init
git add .
git commit -m init

This commands created git repository in the directory, added current directory (helloapp) to the git index, and commited the changes to the local repository.

Next thing to do is creating new application on Heroku: heroku create --stack cedar from helloapp dir. This not only creates new application, but also nicely adds remote git repository. Heroku has few stacks. Stack is OS and stuff installed on it for our applications to run. For now, the only stack that supports Play is called Cedar. It's not the default one, so you need to tell Heroku explicitely that it should be used for your application.

It is also going to ask about your credentials to authenticate you on Heroku. It is done only once, and then your credentials key and token are stored on disk. If you want to delete them, just heroku auth:logout.

Ok, so you have application on Heroku, with git repository added to your remote ones, and you have one app locally which you want now to deploy to the cloud. Well, it couldn't be simplier:
git push heroku master

From now on it only takes some patience. Deployment takes time. When it's finished, it gives you the address, something like http://blooming-stone-5863.herokuapp.com deployed to Heroku in the console.

Go to this address and you'll see your app. Have fun!


James Ward said...

This is awesome! Thanks for posting it! Just one minor clarification. Doing a "heroku login" doesn't really store credentials on disk, it stores an API key / token, just like cookies would store a session id.

-James (Heroku)

kpolo said...

Do you HAVE to do a git push? I.e., does heroku necessarily have to get access to your source code? I thought with a maven build, one can push the POM and heroku can get the libraries (albeit that still means public access to your SVN repository)?

pawelstawicki said...

@kpolo Heroku doesn't have access to your repository. It creates it's own repository for each application, and gives you access to it. So by "git push" you just copy your changes to heroku repository connected with your app. It doesn't have any access to your machine, if that's what you meant. :) Git is quite different than SVN. It's distributed, there is no one central repository.

flurdy said...

@kpolo yes they need a copy of your source code. after all they need to compile and run it.

If paranoid you can create a skinny app with dependencies to your real app jars. But in the end the jars need to be in a public maven repo, and with decompilers there will be full access to your code anyway. So it would just be obfuscation, and a slow painfull one.

opensas said...

Great article... I hope you can write a bit more on your play 2.0 experiences

I'm also playing with play (1.0 so far) on the cloud.

Here's an article on play framework modules and openshift (red hat's Paas) http://playlatam.wordpress.com/2011/12/21/divide-and-conquer-play-framework-modules/