【问题标题】:Don't repeat yourself in Capistrano 3不要在 Capistrano 3 中重复自己
【发布时间】:2014-11-02 18:45:08
【问题描述】:

我当前的命名空间如下所示:

namespace :deploy do
  task :npm_install do
    on roles :all do
      within release_path do
        execute :npm, :install
      end
    end
  end

  task :bower_install do
    on roles :all do
      within release_path do
        execute './node_modules/.bin/bower', :install
      end
    end
  end

  task :build do
    on roles :all do
      within release_path do
        invoke 'deploy:npm_install'
        invoke 'deploy:bower_install'
      end
    end
  end

  after :finishing,  :build
end

我想知道是否有另一种方法可以避免 rolesrelease_path 的重复和嵌套块。

如何写得更简洁?

【问题讨论】:

  • 我在 Internet 上找不到足够的有关 Capistrano 最佳实践和代码重构的信息。

标签: ruby-on-rails capistrano capistrano3


【解决方案1】:

我相信您编写它的方式是 Capistrano 的标准。例如,官方的 Rails 任务也是这样做的:https://github.com/capistrano/rails/blob/master/lib/capistrano/tasks/assets.rake

namespace :assets do
  task :precompile do
    on release_roles(fetch(:assets_roles)) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "assets:precompile"
        end
      end
    end
  end

  task :backup_manifest do
    on release_roles(fetch(:assets_roles)) do
      within release_path do
        # snipped…
      end
    end
  end

  task :restore_manifest do
    on release_roles(fetch(:assets_roles)) do
      within release_path do
        # snipped…
      end
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多