Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在生产服务器上使用capistrano清理旧资产_Ruby On Rails_Ruby_Ruby On Rails 3_Deployment_Capistrano - Fatal编程技术网

Ruby on rails 在生产服务器上使用capistrano清理旧资产

Ruby on rails 在生产服务器上使用capistrano清理旧资产,ruby-on-rails,ruby,ruby-on-rails-3,deployment,capistrano,Ruby On Rails,Ruby,Ruby On Rails 3,Deployment,Capistrano,我在本地使用capistrano预编译我的资产,并在使用rsync上载这些资产之后,使用deploy.rb: namespace :assets do task :precompile, :roles => :web, :except => { :no_release => true } do from = source.next_revision(current_revision) if capture("cd #{latest_release

我在本地使用capistrano预编译我的资产,并在使用rsync上载这些资产之后,使用
deploy.rb:

namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      from = source.next_revision(current_revision)
      if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
        run_locally("rm -rf public/assets/*") 
        run_locally "bundle exec rake assets:precompile"
        find_servers_for_task(current_task).each do |server|
         #clean server assets before upload local new assets
         run_locally "rsync -vr --exclude='.DS_Store' --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{server.host}:#{shared_path}/"
        end
      else
        puts "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end
问题是文件夹
#{shared_path}/assets
随着我的每次部署而快速增长

在上传本地新资产之前,我需要在生产服务器上运行代码
rm-rf public/assets/*
在上传本地新资产之前清理服务器资产


如何执行此操作?

您只需创建一个任务并在:预编译任务之前运行它

task :clean_assets do 
  ...
end

// tell capistrano when you want to run this task
before 'precompile', 'clean_assets

此文件夹非常大时删除资源的修复程序是:

run %Q{cd #{shared_path} && rm -rf assets/* }
在此之前,如果愿意,您已将共享文件夹的路径设置为:

set :shared_path, "path_to_your_shared_folder"
此问题的最终代码如下:

namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      from = source.next_revision(current_revision)
      if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
        run_locally("rm -rf public/assets/*") 
        run_locally "bundle exec rake assets:precompile"
        find_servers_for_task(current_task).each do |server|
         run %Q{cd #{shared_path} && rm -rf assets/* }
         run_locally "rsync -vr --exclude='.DS_Store' --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{server.host}:#{shared_path}/"
        end
      else
        puts "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end

只需将其添加到
config/deploy.rb

# Callbacks
namespace :deploy do
  before :compile_assets, :force_cleanup_assets do
    on release_roles(fetch(:assets_roles)) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, 'assets:clobber'
        end
      end
    end
  end
end

如果在预编译之前运行此任务,则无法检查资产是否已更改。另外,我需要删除生产服务器上的资产的代码。非常感谢。