Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 52 lines (46 sloc) 2.008 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
Capistrano::Configuration.instance(:must_exist).load do |config|
  after 'deploy:assets:precompile', 'deploy:assets:cdn_deploy'

  namespace :deploy do
    namespace :assets do
      desc <<-DESC
Deploy the compiled assets to a CDN. RSync the shared directory to asset_cdn_host.
This operation happens on a single host.
DESC
      task :cdn_deploy, :roles => :web do
        unless fetch(:skip_cdn_deploy, false)
          # sync assets
          server = find_servers_for_task(current_task).first
          asset_dir = "#{shared_path}/assets"
          ssh_env = fetch(:ssh_env, "-e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'")
          run "rsync -av #{ssh_env} #{asset_dir} #{asset_cdn_host}", :hosts => server
        end
      end

      # Override the capistrano default task
      task :precompile, :roles => :web, :except => { :no_release => true } do
        if force_asset_compilation? || assets_dirty?
          run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
          set :skip_cdn_deploy, false
        else
          set :skip_cdn_deploy, true
          logger.info "Skipping asset pre-compilation because there were no asset changes"
        end
      end

      def force_asset_compilation?
        fetch(:always_compile_assets, false) || ENV['COMPILE_ASSETS'] == 'true'
      end

      def cached_copy
        File.join(shared_path, fetch(:repository_cache, "cached-copy"))
      end

      def assets_dirty?
        r = safe_current_revision
        return true if r.nil?
        from = source.next_revision(r)
        asset_changing_files = ["vendor/assets/", "app/assets/", "lib/assets", "Gemfile", "Gemfile.lock"]
        asset_changing_files = asset_changing_files.select do |f|
          File.exists? f
        end
        capture("cd #{cached_copy} && #{source.local.log(current_revision, source.local.head)} #{asset_changing_files.join(" ")} | wc -l").to_i > 0
      end
    end
  end
end
Something went wrong with that request. Please try again.