在使用Capistrano部署代码时如何替换共享文件?(How to replace a shared file when deploying code with Capistrano?)

系统教程 行业动态 更新时间:2024-06-14 16:53:07
在使用Capistrano部署代码时如何替换共享文件?(How to replace a shared file when deploying code with Capistrano?)

更新:TL; DR似乎没有内置的方法来实现这一点,因此自定义任务是一个简单的解决方案。

Capistrano提供了在所有版本上共享文件和目录的工具。 这很方便,甚至可以为文件提供一些安全性,这些文件不容易更改(或者必须在不同版本中保持不变),例如数据库配置文件。

但是当谈到替换或只更新其中一个共享文件时,我最终会直接在目标计算机上手动完成。 我想对此进行改进,例如通过要求Capistrano在部署时覆盖部分或全部共享文件。 一种具有一定粒度的--force标志。

我不知道有任何这样的设施,到目前为止在我的搜索中失败了。 任何指针?


考虑此事

这个设施不存在的原因之一(除了我没有找到它!)是因为它可能比它看起来更难。 例如,假设我们有一个共享数据库配置文件,出于安全原因(常见做法)我们将其从版本控制中排除。 当前版本依赖于DB配置的版本1。 下一版本需要DB配置的第2版。 如果部署进展顺利,一切都很好。 在使用新版本(例如回归)发生错误后回滚时会变得更难,因为版本1 必须可用。


这种自动化既凉爽又方便,但也很危险。 但我手头有实用的用例。

Update: TL;DR there seems to be no built-in way to achieve this, so a custom task is an easy solution.

Capistrano provides facilities to share files and directories over all releases. This is convenient and provides even some safety on files that should not be easily changed (or must remain the same across releases), e.g. a database configuration file.

But when it comes to replace or just update one of these shared files, I end up doing it manually, directly on the target machine. I would like to improve on that, for instance by asking Capistrano to overwrite some or all shared files when deploying. A kind of --force flag with some granularity.

I am not aware of any such kind of facility, and failing so far in my search. Any pointer?


Thinking about it

One of the reason why this facility does not exist (except that I did not find it!) is that it may be harder than it looks. For example, let's assume we have a shared database configuration file, and we exclude it from version control for security reason (common practice). Current release relies on version 1 of the DB configuration. The next release requires version 2 of the DB configuration. If the deployment goes well, everything's good. It gets harder when rolling back after some error with the new release (e.g. a regression), as version 1 must then be available.


Such automation would be cool and convenient, but dangerous as well. Yet I have practical use cases at hand.

最满意答案

我创建了一个template方法来执行此操作。 例如,我可以有这样的任务:

task :create_database_yml do on roles(:app, :db) do within(shared_path) do template "local/path/to/database.yml.erb", "config/database.yml", :mode => "600" end end end

然后我有一个database.yml.erb模板,它使用fetch(:database_password)类的东西来填充适当的值。 您可以使用Capistrano中的ask方法来提示这些值,以便它们永远不会被提交。

template的实现可以非常简单:您只需要读取文件,通过ERB传递,然后使用Capistrano的upload! 将结果放在服务器上。

我的版本比你的版本要复杂得多,但万一你很好奇:

https://github.com/mattbrictson/capistrano-mb/blob/7600440ecd3331945d03e059368b75849857f1fb/lib/capistrano/mb/dsl.rb#L104

I created a template method to do this. For example, I could have a task like this:

task :create_database_yml do on roles(:app, :db) do within(shared_path) do template "local/path/to/database.yml.erb", "config/database.yml", :mode => "600" end end end

And then I have a database.yml.erb template that uses things like fetch(:database_password) to fill in appropriate values. You can use the ask method in Capistrano to prompt for these values so they are never committed.

The implementation of template can be very simple: you just need to read the file, pass it through ERB, and then use Capistrano's upload! to place the results on the server.

My version is a little more complicated than yours probably needs to be, but in case you are curious:

https://github.com/mattbrictson/capistrano-mb/blob/7600440ecd3331945d03e059368b75849857f1fb/lib/capistrano/mb/dsl.rb#L104

更多推荐

本文发布于:2023-04-05 21:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/fe38b1df762a2faa23f79962e319c7be.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   文件   replace   Capistrano   deploying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!