Since Ruby On Rails 3 released I had a problems with deploy configuring like assets precompile. Of course if you deploy application to powerful host you won't have any problems and deployment to such host will be standart and simple. But if you have server with not much RAM (less 1 Gb) you will have to waiting long time till assets precompiled. So, here I will tell how to avoid precompiling assets on the server and decrease time for deployment.
If you haven't already known free ruby hosting I would recomend you to use heroku. It's a beatiful freehosting which provides deploy Rack/Ruby On Rails applications. I won't describe how it works here because you can find a lot of information about it on the oficial site.
I have been use free account for a months for my blog which you are reading now and I won't have any issues with server fails, long loading or like that. Sometimes it was about 1000 visitors per hour and everything has done. So I think it will be enough free account for your starting services or staging/qa environments. If you haven't enough memory you can buy web dyno for 36$ per month. Also you are able to install add-ons - it's a wonderful heroku's feature which saves your time with installing and configuring additional applications required for your application such as:
NOTE: there are restrictions for every add-on.
I hope you have already signed up here and ready to the next step.
There are many variants to precompile assets and connect them with Rails 3 application:
I prefer 3rd variant because heroku doesn't allow to save any files on the instance. Also 1 free web dyno has only 512 MB memory. To save assets in the bucket we should choose server with API for our issue. There are a lot of services in the internet: AWS S3, Dropbox, Rackspace Cloud Files and etc. It will be simple to use Dropbox for our problem: you have precompiled assets, you just copy them to Dropbox public folder, say to your application where it should look for assets and that's it! But I prefer use S3 because I calculated that it will be cheaper for me in the feature.
NOTE: there are variants of regions where to store bucket. So choose nearest for your feature visitors to have better response from server
Please, wait for status changed to Deployed and State to Enabled. It will take a few minutes (usual it is 5-10 minutes)
To upload assets in the bucket I use gem jammit-s3. It is simple to configure and to use.
s3_bucket: YOUR_BUCKET_HERE s3_access_key_id: <%= ENV['AWS_KEY'] %> s3_secret_access_key: <%= ENV['AWS_SECRET'] %> s3_upload_files: - public/assets/**/*
Pay attention on config.action_controller.asset_host, it should be your bucket url!
Add public/assets to .gitignore:
In production mode file manifest.yml is required. It generates to the /public/assets folder when assets are precompiling. I see the nice solution in changing path for manifest.yml file. Go to the config/environments/production.rb and edit it like this way:
# ... # Defaults to Rails.root.join("public/assets") config.assets.manifest = 'public/manifest' # ...
We have ready to deploy application. Let's precompile assets:
RAILS_ENV=production rake assets:precompile --trace
Upload assets to S3 is very simple:
bundle exec jammit-s3
Create heroku application:
Make commit, and push it to the heroku:
git commit -am 'assets precompiled' git push heroku master
Default DB for heroku application is postgresql. If you would like to change it install preferred for you add-on.
Of course you should run migrations if your application has it:
heroku run rake db:migrate heroku run rake db:seed # if you have required seeds
I hope this post will be useful not only for me. Thanks for reading!Andrey Koleshko 19 March 2012