即使资产没有变化,也会发生Rails资产预编译

时间:2016-06-09 04:37:13

标签: ruby-on-rails ruby-on-rails-4 aws-opsworks

我正在AWS Opsworks上运行rails堆栈。在before_symlink.rb deploy hook中,我有以下代码。

rails_env = new_resource.environment["RAILS_ENV"]
shared_path = "#{new_resource.deploy_to}/shared"

# create shared directory for assets, if it doesn't exist
directory "#{shared_path}/assets" do
  mode 0770
  action :create
  recursive true
  not_if do
    Dir.exists?("#{shared_path}/assets")
  end
end

# symlink current deploy's asset folder to shared assets each deploy
link "#{release_path}/public/assets" do
  to "#{shared_path}/assets"
end

# precompile assets into public/assets (which is symlinked to shared assets folder)
execute "rake assets:precompile" do
  cwd release_path
  command "bundle exec rake assets:precompile --trace"
  environment 'RAILS_ENV' => rails_env
end

问题是,即使没有变化,资产也会一直预编译,尽管它们位于符号链接的共享文件夹中。我在Rails 4.1.2上运行,我猜Rails是否足够聪明,只能在Rails 4之后编译已更改的资产?为什么会这样?

更新

现在已经解决了。

我错过了symlinking / tmp / cache到/ shared / tmp / cache。这是sprockets存储缓存文件的地方。

更新了代码。

# Precompile assets. Assets are compiled into shared/assets and shared between deploys.
rails_env = new_resource.environment["RAILS_ENV"]
shared_path = "#{new_resource.deploy_to}/shared"

# create shared directory for assets, if it doesn't exist
directory "#{shared_path}/assets" do
  mode 0770
  action :create
  recursive true
  not_if do
    Dir.exists?("#{shared_path}/assets")
  end
end

#create shared directory to store sprockets cache
directory "#{shared_path}/tmp/cache" do
  mode 0770
  action :create
  recursive true
  not_if do
    Dir.exists?("#{shared_path}/tmp/cache")
  end
end

# symlink current deploy's asset folder to shared assets each deploy
link "#{release_path}/public/assets" do
  to "#{shared_path}/assets"
end

# symlink current deploy's sprockets cache  folder to shared cache folder on each deploy
link "#{release_path}/tmp/cache" do
  to "#{shared_path}/tmp/cache"
end

# precompile assets into public/assets (which is symlinked to shared assets folder)
execute "rake assets:precompile" do
  cwd release_path
  command "bundle exec rake assets:precompile --trace"
  environment 'RAILS_ENV' => rails_env
end

1 个答案:

答案 0 :(得分:1)

OpsWorks在每次部署时都会重建您的整个rails应用程序,并保留许多备份副本。它应该在如下路径中创建它:

/srv/www/yourappname/current

您可以通过ssh将其确认到您的服务器中。如果你去路径

ls -la / srv / www / yourappname / releases

您应该会看到一些带有日期时间戳记名称的文件夹。 如果您查看内容,它们将包含您的整个rails应用程序。 如果在部署期间出现问题,则可以轻松回滚。

由于每个文件夹基本上都是您的应用程序的新安装,因此需要进行资产预编译,否则当前版本将没有任何资产。这种类型的部署不只是具有相同的目录 git pull origin master

更新:看起来这是带有Sprockets的issue已在此pull request中解析,因此请确保您的gem sprockets版本包含此commit。看起来它包含在v4.0.0beta1& v4.0.0beta2请尝试或不尝试,请尝试降级到>=2.12.4 as per this comment