返回首页 - Notes - 2016

Mina 多环境部署


安装

Mina 多环境部署需要借助插件 mina-multistage

  1. 安装 Minagem install mina
  2. 安装 mina-multistage 插件: gem install mina-multistage
  3. 初始化 Mina 配置文件:bundle exec mina init
  4. 修改 Mina 配置文件:vim config/deploy.rb
    require 'mina/multistage'  #  在顶部添加这一行,必须要先加了这一行才能正常执行下一步
    # ...
    
    task setup: :environment do  # setup 需要改成这样
      # ...
    end
    
    task deploy: :environment do  # deploy 需要改成这样
      # ...
    end
    
  5. 初始化 mina-multistage 配置文件:bundle exec mina multistage:init

mina-multistage 会生成一个 config/deploy 文件夹,下面每一个文件代表一个部署环境,可按需设置


配置

文件规划

部署指令

  1. 初始化服务器目录结构:bundle exec mina prod setup
  2. 执行部署:bundle exec mina prod deploy

部署到不同的环境,只需将上述命令中的 prod 替换成指定环境的名字即可


典型配置示例

主部署文件:config/deploy.rb

require 'mina/multistage'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'


set :repository, 'git@git.coding.net:ruchee/rails_demo2.git'

set :shared_dirs,  fetch(:shared_dirs, []).push('log', 'tmp/cache', 'tmp/pids', 'tmp/sockets')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')


task :environment do
  invoke :'rvm:use', 'default'
end

task setup: :environment do
end

task deploy: :environment do
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    on :launch do
      in_path(fetch(:current_path)) do
        command %{mkdir -p tmp/}
        command %{touch tmp/restart.txt}
        invoke :'puma_restart'
      end
    end
  end
end

task puma_restart: :environment do
  puma_pid = "#{fetch(:current_path)}/tmp/pids/puma.pid"
  command %[
    if [ -e '#{puma_pid}' ]; then
      pumactl -F config/puma.rb stop
      pumactl -F config/puma.rb start
    else
      pumactl -F config/puma.rb start
    fi
  ]
end

开发机部署配置:config/deploy/dev.rb

set :domain, 'ruchee@sites.ruchee.com'
set :deploy_to, '/home/ruchee/www/rails_demo'
set :branch, 'dev'

测试机部署配置:config/deploy/test.rb

set :domain, 'ruchee@sites.ruchee.com'
set :deploy_to, '/home/ruchee/www/rails_demo'
set :branch, 'test'

生产环境部署配置:config/deploy/prod.rb

set :domain, 'ruchee@sites.ruchee.com'
set :deploy_to, '/home/ruchee/www/rails_demo'
set :branch, 'prod'

date:2016-11-10