如何使用Capistrano 2部署Rails 4并在本地预编译资产

时间:2022-11-06 20:37:56

Recently I upgraded an application from Rails 3 to Rails 4. In the deploy scripts I precompile the assets locally and then rsync them up to the server(s). In Rails 4 the asset pipeline now produces manifest- < random > .json instead of a manifest.yml. Since the manifest files are named differently, this adds multiple manifest.json files to the shared assets directory. The application then picks up the wrong manifest file, and serves old assets.

最近,我将一个应用程序从Rails 3升级到Rails 4。在部署脚本中,我在本地预编译资产,然后将它们同步到服务器。在Rails 4中,资产管道现在生成manifest- < random > .json,而不是manifest.yml。由于清单文件的命名方式不同,这增加了多个清单。json文件到共享资产目录。然后应用程序获取错误的清单文件,并服务于旧的资产。

I have read about various issues related to this in some github pull request threads:

我在一些github的拉取请求线程中读到了与此相关的各种问题:

My options seem to be:

我的选择似乎是:

  1. Don't share the asset directory.

    不要共享资产目录。

    This would break old clients asking for old resources.

    这将使老客户对旧资源的需求减少。

  2. Switch to compiling assets on the servers.

    切换到编译服务器上的资产。

    This would add complexity to the server.

    这将增加服务器的复杂性。

  3. Move the manifest file outside of the shared asset directory.

    将清单文件移到共享资产目录之外。

    I have since learned that this option was removed in Rails 4.

    我后来了解到这个选项在Rails 4中被删除了。

Are there other solutions to this problem?

这个问题还有别的解决办法吗?

1 个解决方案

#1


2  

I found the best answer after looking at the standard capistrano rails asset precompile task. I added a command to the local precompile task that moves the old asset manifest to the current release as asset_manifest.json. This leaves only one manifest when the new one is uploaded.

在查看了标准的capistrano rails资产预编译任务之后,我找到了最好的答案。我向本地预编译任务添加了一个命令,该任务将旧的资产清单作为asset_manifest.json移动到当前版本。这只留下一个显式,当新显式被上载时。

run "mv -- #{shared_manifest_path.shellescape} #{current_path.to_s.shellescape}/assets_manifest#{File.extname(shared_manifest_path)}".compact

“mv——# { shared_manifest_path运行。shellescape } # { current_path.to_s.shellescape } / assets_manifest # { File.extname(shared_manifest_path)}”.compact

Moving the manifest-.json to the current_dir as asset_manifest.json allows capistrano to restore the correct manifest file on rollback.

清单——移动。json到current_dir作为asset_manifest。json允许capistrano在回滚时恢复正确的清单文件。

#1


2  

I found the best answer after looking at the standard capistrano rails asset precompile task. I added a command to the local precompile task that moves the old asset manifest to the current release as asset_manifest.json. This leaves only one manifest when the new one is uploaded.

在查看了标准的capistrano rails资产预编译任务之后,我找到了最好的答案。我向本地预编译任务添加了一个命令,该任务将旧的资产清单作为asset_manifest.json移动到当前版本。这只留下一个显式,当新显式被上载时。

run "mv -- #{shared_manifest_path.shellescape} #{current_path.to_s.shellescape}/assets_manifest#{File.extname(shared_manifest_path)}".compact

“mv——# { shared_manifest_path运行。shellescape } # { current_path.to_s.shellescape } / assets_manifest # { File.extname(shared_manifest_path)}”.compact

Moving the manifest-.json to the current_dir as asset_manifest.json allows capistrano to restore the correct manifest file on rollback.

清单——移动。json到current_dir作为asset_manifest。json允许capistrano在回滚时恢复正确的清单文件。