Chef with rails: Handling asset precompiling during deploy
-
24-05-2021 - |
Question
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?
Solution
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