Ruby-on-rails – Assets precompile not being run on heroku cedar for rails 3.2 app

asset-pipelineherokurubyruby-on-railsruby-on-rails-3

Started working on a new application this week that is running the latest rails 3.2.2 with the asset pipeline. For the assets I would like the assets to be compiled when pushing the app to heroku (rather than having to manually compile and store compiled assets in the repo). The docs suggest this should happen automatically.

The problem I have is when I run git push heroku master it does not seem to run the rake assets:precompile task at all. The assets are never compiled. Here is the output I get:

-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.1.rc.7
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       Using rake (0.9.2.2)
       .......
       .......
     Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
-----> Discovering process types
       Procfile declares types      -> web
       Default types for Ruby/Rails -> console, rake, worker
-----> Compiled slug size is 61.7MB
-----> Launching... done, v30
       [appname] deployed to Heroku

I have the asset pipeline enabled in my application.rb

require File.expand_path('../boot', __FILE__)

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"

Bundler.require *Rails.groups(:assets) if defined?(Bundler)

module Appname
  class Application < Rails::Application
    # ...

    # Enable the asset pipeline
    config.assets.enabled = true

    # Do not boot the app when precompiling assets
    config.assets.initialize_on_precompile = false
  end
end

Anyone know what might be causing the problem? I'm happy to gist more code if need be. If I run heroku info it shows that i'm running on the Cedar stack.

I had to turn on config.assets.compile = true to stop receiving 500 errors in production, although this compiles the assets at runtime (which I do not want).

Best Answer

This seems to be solved now. I believe it was caused by the Rakefile not loading the assets:precompile task in production.

The rakefile was loading up tasks for both the cucumber and rspec gems which are not bundled into production. As a result the assets:precompile task was not available in production so heroku didn't attempt to run it.

I wrapped the test tasks in a environment check conditional like so:

if %(development test).include?(Rails.env)
  require 'rspec/core'
  require 'rspec/core/rake_task'
end
Related Topic