首页 文章

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

提问于
浏览
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?

1 回答

  • 1

    set :scm, 'myscm' is deprecated . 直到下一个主要版本的Capistrano(4.0),there is a class检查已通过 install_plugin 安装的SCM,如果没有,则检查 set :scm 定义 . 如果已调用 install_plugin ,则忽略并删除 set :scm .

    install_plugin 仅注册插件 . 它看起来像from the code,如果安装了两个插件,Capistrano将运行两个插件 .

    因此,简而言之,Capistrano不支持基于环境选择多个SCM . 您可以尝试的最接近的事情是使用环境变量有条件地加载Capfile中的SCM . 就像是:

    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

相关问题