简体   繁体   中英

Chef with rails: Handling asset precompiling during deploy

If you're using rails with Sprockets and Opscode Chef, how are you handling asset precompilation during deployment? I want to keep my git history clean, so I don't want do precompile them locally and then commit them to the repository.

The easiest way seems to be to add them to the application cookbook's migration command, but that seems nasty to me. Asset precompilation should be separate from database migrations. Any other suggestions on how to handle this?

If you are using the deploy_revision resource, you can stick the rake task to do the asset precompiling into the before_restart block.

Here is the snippet of code that is in my deploy_revision resource. Since I am using RVM, I have installed Fletcher Nichol's amazing RVM cookbook of awesomeness. You could replace this with a ruby-block resource.

Checkout a more complete example in my gist .

 app = node[:rails][:app_name]
 before_restart do
   rvm_shell "assets precompile" do
     ruby_string "#{app[:ruby_ver]}@#{app[:gemset]}"
     cwd release_path
     user app[:deploy_user]
     group app[:deploy_user]

     # TODO I could not set the environment via the builtin command. Does not look like it is getting passed to popen4
     # So instead of `environment "RAILS_ENV" => app[:environment]` I have it in the code block
     code %{
       export RAILS_ENV=#{app[:environment]}
       bundle exec rake assets:precompile
     }
   end
 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM