How to precompile assets with Chef?

后端 未结 4 1905
慢半拍i
慢半拍i 2021-02-02 00:27

OpsWorks isn\'t precompiling assets on deploy. I found this recipe in this thread but I think it\'s not complete though, or missing something because I get an error about

4条回答
  •  梦如初夏
    2021-02-02 01:04

    I know very little about OpsWorks and Chef, but here's what I did to get it working.

    First, I had to create a rails recipe that runs during the setup event to create the symlink directory for the assets. This sits in a public repo that OpsWorks can access.

    cookbooks/rails/recipes/symlink_assets.rb:

    node[:deploy].each do |application, deploy|
      Chef::Log.info("Ensuring shared/assets directory for #{application} app...")
    
      directory "#{deploy[:deploy_to]}/shared/assets" do
        group deploy[:group]
        owner deploy[:user]
        mode 0775
        action :create
        recursive true
      end
    end
    

    Then, in my app, I had to create deploy/before_migrate.rb:

    Chef::Log.info("Running deploy/before_migrate.rb...")
    
    Chef::Log.info("Symlinking #{release_path}/public/assets to #{new_resource.deploy_to}/shared/assets")
    
    link "#{release_path}/public/assets" do
      to "#{new_resource.deploy_to}/shared/assets"
    end
    
    rails_env = new_resource.environment["RAILS_ENV"]
    Chef::Log.info("Precompiling assets for RAILS_ENV=#{rails_env}...")
    
    execute "rake assets:precompile" do
      cwd release_path
      command "bundle exec rake assets:precompile"
      environment "RAILS_ENV" => rails_env
    end
    

    This get called during the deployment process and compiles the assets.

提交回复
热议问题