Deploying a Rails app on Heroku (paperclip, gems, yml)

I just moved my PlaygroundsRUs site from AWS to Heroku, and I couldn’t be happier. My first full month bill from Amazon was about $75, and $74 of that (99% of the total cost) was for running an instance. Harlan told me about Heroku after he deployed his ForkThis demo on there.

Most of the transition was smooth, but there were a few hiccups on the way. One of them still hasn’t been resolved (one of the plugins is having conflict with PostgresSQL, which is used by Heroku). I will enumerate what I had to go through so that it might be easier for you.

1. Secret YAML files

I have a public GitHub account for deploying on AWS using ec2onrails. Since it’s public and anyone can see it, I had to omit sensitive .yml files in config directory that contained passwords and keys. It’s easily done by specifying those files in .gitignore and listing them in :nonvc_configs in config/deploy.rb used by ec2onrails.

I found out that for Heroku, the same can be achieved by creating another branch, including those files, merging with master, and pushing it to Heroku. So, the following lines should do the trick.

git checkout production
[remove those yml files from .gitignore]
git merge master
git push heroku production:master

2. gems

It pays to read through documentations. I thought it might be easier to install/unpack gems in vendor directory, since I wasn’t sure how gems would be installed for my app on Heroku. But, they made it very easy. Just follow direction here and list those gems required by your app in .gems file. Can’t get any easier than that.

3. paperclip

Last is paperclip. I use it to upload pictures for each playground object. Since you can’t write direct on Heroku’s file system, it makes sense to use Amazon’s s3. Heroku document actually says that Heroku runs on the Amazon platform and thus s3 transfer is free.

Just like ec2onrails, you can use s3.yml to specify access_key and access_secret. You could also follow direction on Heroku here, but I found out that Heroku’s example is missing one critical parameter, :path. Without :path parameter, you will get an error. There is an excellent example of using paperclip on Heroku by Pedro. It’s definitely worth checking out.

So, my model that accepts attachments have the following lines.

has_attached_file :data,
 :styles => {
 :thumb => "50x50#",
 :large => "600x400#"
 },
 :storage => :s3,
 :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
 :path => ":attachment/:id/:style.:extension"

While my s3.yml looks like the following.

staging:
 access_key_id: [Your Key]
 secret_access_key: [Your Secret]
 bucket: [Your bucket name]

production:
 access_key_id: [Your Key]
 secret_access_key: [Your Secret]
 bucket: [Your bucket name]

Oh, and there is Firefox plugin – S3Fox – for managing Amazon s3. It’s SO good. You gotta check out. I love the whole rails community and Firefox. They rock!

Post to Twitter

3 thoughts on “Deploying a Rails app on Heroku (paperclip, gems, yml)

  1. Hi there, thanks for this post, it’s very helpful although I have a question regarding keeping secret yaml files off github.

    You say that the best way is to do the following:

    git checkout production
    [remove those yml files from .gitignore]
    git merge master
    git push heroku production:master

    Is there some kind of rake task that will keep this production branch synched up with master or do you just have to remember to do it yourself on every deploy?

    Thanks again, it’s a really neat little tip.

    • As long as you don’t merge production to master, you should be okay, and I don’t know if there is a rake task, but it’s a good idea if you can create a rake task yourself.

      Because I use facebooker and I keep facebooker.yml away from master branch. So I end up doing a lot of work and test in production branch, because running local server needs facebooker.yml. When I am satisfied, I go back to master branch, commit the changes, push to github, go to production branch, merge from master and push to Heroku. Perhaps I should write a little shell script to automate this process……some day. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax