Capistrano 3:处理不同部署流程的方法

时间:2017-05-24 08:12:21

标签: capistrano3

我是Capistrano的新手,但我已成功将其用于自定义部署。我必须将Moodle部署到AWS中具有自动扩展组的群集以及一个或多个静态服务器。

效果很好!我已设法通过自定义任务更改流程,以使我的网站进行维护并清除缓存而不会出现问题:

namespace :moodle do

  desc 'Save config.php from current release directory'   task :'save-config'
    on roles(:web) do
      execute :sudo, :cp, shared_path.join('config.php'), release_path
      execute :sudo, :chown, 'www-data-aulatp:www-data', release_path.join('config.php') 
    end
  end

  desc 'Copy config.php to release directory'   task :'restore-config' do
    on roles(:web) do
      execute :sudo, :cp, shared_path.join('config.php'), release_path
      execute :sudo, :chown, 'www-data-aulatp:www-data', release_path.join('config.php') 
    end
  end

  desc 'Enable maintenance mode on Moodle site'   task :'enable-maintenance' do
    on roles(:admin) do
      execute :sudo, '-u', 'www-data-aulatp', '/usr/bin/php7.0', current_path.join('admin', 'cli', 'maintenance.php'), '--enable'
    end
  end

  desc 'Disable maintenance mode on Moodle site'   task :'disable-maintenance' do
    on roles(:admin) do
      execute :sudo, '-u', 'www-data-aulatp', '/usr/bin/php7.0', current_path.join('admin', 'cli', 'maintenance.php'), '--disable'
    end
  end

  desc 'Purge all internal Moodle caches'   task :'purge-caches' do
    on roles(:admin) do
      execute :sudo, '-u', 'www-data-aulatp', '/usr/bin/php7.0', current_path.join('admin', 'cli', 'purge_caches.php')
    end   
  end
end

before   'deploy:starting',    'moodle:save-config'
before   'deploy:updated',     'moodle:enable-maintenance' 
after    'deploy:updated',     'moodle:restore-config' 
after    'deploy:finished',    'moodle:enable-maintenance' 
after    'deploy:finished',    'moodle:disable-maintenance' 
after    'deploy:finished',    'moodle:purge-caches'

问题是,有时我需要快速部署,一个不需要启用和禁用维护模式或清除缓存的小补丁。

卡宾斯特拉是否可行?

作为一种可行的替代方案,我一直在寻找capistrano-patch,这是一种在不创建完整部署过程的情况下将简单补丁部署到每个服务器的方法。看起来它已经好几年没有更新了,我想它不适用于capistrano 3.任何类似的想法与Capistrano 3做HOTFIX?

1 个答案:

答案 0 :(得分:0)

您可以使用环境变量来声明需要绕过某些内容。例如,您可以使用NO_CACHE_PURGE=true cap production deploy运行部署,然后使用以下内容包装相关代码:

desc 'Purge all internal Moodle caches'
task :'purge-caches' do
  if ENV['NO_CACHE_PURGE'].nil?
    on roles(:admin) do
      execute :sudo, '-u', 'www-data-aulatp', '/usr/bin/php7.0', current_path.join('admin', 'cli', 'purge_caches.php')
    end
  else
    puts 'Skipping purge-caches due to env variable'
  end
end

如果你很了解Ruby,并且想要自动化它,你可能会编写一个简化其中一些方法的方法(以下代码未经测试):

def disableable_task(*args)
  if ENV[args[:env_name]].nil?
    task(args)
  else
    puts "Skipping #{args[0]} due to #{args[:env_name]} environment variable being set"
end

desc 'Purge all internal Moodle caches'
disableable_task :'purge-caches', env_name: 'NO_CACHE_PURGE' do
  on roles(:admin) do
    execute :sudo, '-u', 'www-data-aulatp', '/usr/bin/php7.0', current_path.join('admin', 'cli', 'purge_caches.php')
  end
end