【问题标题】:How do you handle deploying rails applications with submodules?您如何处理使用子模块部署 Rails 应用程序?
【发布时间】:2024-06-18 06:35:01
【问题描述】:

我最近将我的几个插件变成了子模块,并意识到当你“git clone”一个存储库时,子模块目录将是空的。这对于共同开发者初始化他们的子模块和更新是有意义的。

但是,当我使用 capistrano 部署时,子模块代码显然不会被部署,这会导致问题。我可以进入发布分支并在那里初始化并更新模块,但这显然不是一个理想的解决方案。

有人对如何处理这个问题有建议吗?它像 capistrano 任务一样简单吗?

我在制作方面有点菜鸟。

谢谢!

【问题讨论】:

    标签: git plugins capistrano deployment git-submodules


    【解决方案1】:

    根据this recent thread,capistrano 应该能够初始化和更新您的子模块:

    set :git_enable_submodules,1
    

    如果您的 .gitmodules 条目是最新的,则在 config/deploy.rb 中应该足够了。
    您可能需要to patch Capistrano (lib/capistano/recipes/deploy/scm/git.rb) 来确保您的子模块被包含在内。

        def checkout(revision, destination)
          git      = command
    
          branch   = head
    
          fail "No branch specified, use for example 'set :branch, \"origin/master\"' in your deploy.rb" unless branch
    
          if depth = configuration[:git_shallow_clone]
            execute  = "#{git} clone --depth #{depth} #{configuration[:repository]} #{destination} && " 
          else
            execute  = "#{git} clone #{configuration[:repository]} #{destination} && " 
          end
    
          execute += "cd #{destination} && #{git} checkout -b deploy #{branch}" 
    
          if submodules = configuration[:git_enable_submodules]
            execute += " && git-submodule init &&" 
            execute += "git-submodule update" 
          end
    
          execute
        end
    

    如果你有nested submodules,你需要:

    gem sources -a http://gems.github.com
    $ sudo gem install morhekil-capistrano-deepmodules
    

    只需在您的部署配置中使用它:

    需要'capistrano/deepmodules'

    gem 会自动处理所有其余的事情。
    你可以从你的配置中删除:git_enable_submodules,gem 不会注意它——如果你需要它,你已经在说你想要启用子模块。

    还有一个需要注意的细节 - 目前 gem 只支持远程缓存策略。这意味着您必须在您的 config 中添加以下行:

    set :deploy_via, :remote_cache
    

    它启用了远程缓存,这确实是您想要做的事情 - 如果您没有服务器端缓存,那么部署包含大量子模块和其他东西的大型代码库确实是一种麻烦的体验。

    【讨论】:

      【解决方案2】:

      如果没有这个选项,set :git_enable_submodules, 1 本身就无法工作:

      set :deploy_via, :remote_cache`
      

      这似乎没有记录在任何地方,我花了一段时间才弄清楚。无论如何,即使没有子模块,拥有该选项通常也很好。

      【讨论】:

        【解决方案3】:

        使用this commit,Capistrano 支持 Git 子模块和内置的 --recursive 选项。要启用 Git 子模块支持,请将其添加到您的 deploy.rb 文件中:

        set :git_enable_submodules, true

        如果您使用recursive Git submodules,也请添加:

        set :git_submodules_recursive, true

        【讨论】:

          最近更新 更多