尝试在 Capistrano 3.8.1 上设置自定义 SCM

Trying to set custom SCM on Capistrano 3.8.1

我正在配置自定义 SCM,因为我不需要开发本地环境中的默认 git,但我想触发自定义逻辑,主要基于从 source_directory。 如文档 (http://capistranorb.com/documentation/advanced-features/custom-scm/) 中所述,我编写了一个扩展 Capistrano::Plugin 的模块,并设置了所需的方法来处理部署 Capistrano 流程使用的自定义 SCM 实现。

除此之外,当我输入 config/deploy/<environment>.rb 条目时:

set :scm, :<custom plugin name>

Capistrano 继续使用默认的 git scm,即使没有声明。

在我的 Capfile 中加载如下:

require_relative 'scm/local.rb'
install_plugin Capistrano::LocalPlugin
require 'capistrano/git
install_plugin Capistrano::SCM::Git

这里还有自定义SCM的模块:

 require 'capistrano/scm/plugin'

 module Capistrano
    class Capistrano::SCM::LocalPlugin < ::Capistrano::Plugin
       def set_defaults
        set_if_empty :source_dir, 'non-exisisting-dir'
       end

    def define_tasks
        namespace :local do
          task :create_release do
              run_locally do
                  execute :mkdir, '-p', :'tmp'
                  execute "cd #{fetch(:source_dir)} && tar -cz --exclude tests --exclude vendor --exclude .git --exclude node_modules --exclude tmp/#{fetch(:release_timestamp)}.tar.gz -f tmp/#{fetch(:release_timestamp)}.tar.gz ."
              end

              on release_roles :all do
                  execute :mkdir, '-p', release_path
                  upload! "tmp/#{fetch(:release_timestamp)}.tar.gz", "#{release_path}/#{fetch(:release_timestamp)}.tar.gz"
                  execute "tar -xvf #{release_path}/#{fetch(:release_timestamp)}.tar.gz --directory #{release_path}"
                  execute "rm #{release_path}/#{fetch(:release_timestamp)}.tar.gz"
              end

              run_locally do
                  execute "rm -rf tmp"
              end
          end

          desc 'Determine the revision that will be deployed'
          task :set_current_revision do
              run_locally do
                  set :current_revision, capture(:git, " --git-dir #{fetch(:source_dir)}/.git rev-parse --short #{fetch(:branch)}")
              end
          end
        end
    end

    def register_hooks
        after 'deploy:new_release_path', 'local:create_release'
    end
end

结束

有谁知道为了让 Capistrano 使用我的 scm 而不是默认的 git 可以使用哪个黑魔法?

set :scm, 'myscm' is deprecated. Until the next major version of Capistrano (4.0), there is a class 检查是否已通过 install_plugin 安装了 SCM,如果没有,则检查 set :scm 定义。如果调用了 install_plugin,则忽略并删除 set :scm

install_plugin 只注册插件。在我看来 from the code 如果安装了两个插件,Capistrano 将 运行 这两个插件。

所以,简而言之,Capistrano不支持根据环境选择多个SCM。您可以尝试的最接近的方法是使用环境变量有条件地将 SCM 加载到您的 Capfile 中。类似于:

if ENV['CAP_SCM'] == 'local'
  require_relative 'scm/local.rb'
  install_plugin Capistrano::LocalPlugin
else
  require 'capistrano/git'
  install_plugin Capistrano::SCM::Git
end

这里记录了所有内容:https://github.com/capistrano/capistrano/blob/master/UPGRADING-3.7.md