使用Capistrano自动化部署

环境:
Centos 7
Capistrano v3
ruby 2.4.1
rails 5.2.3

Installation

install as Ruby Gem

gem install capistrano

Usage in a Rails project

1
2
3
4
group :development do
gem "capistrano", "~> 3.10", require: false
gem "capistrano-rails", "~> 1.3", require: false
end

bundle install

capistrano-rails

  • Asset Pipeline Support
    cap deploy:migrate
  • Database Migration Support
    cap deploy:compile_assets

    Usage

bundle exec cap install

会生成如下结构。

Capfile
config/deploy.rb
config/deploy/production.rb
config/deploy/staging.rb
lib/capistrano/tasks

Capfile 实际上是ruby文件
主要作用:require ruby module,install_plugin, and

1
2
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

config/deploy.rb
配置capistrano task的变量,定义任务,hooks。

config/deploy/*.rb
定义各部署环境的serverroles,即定义各服务器承担了什么角色或者各角色的服务器。
config/deploy/production.rb 执行的时候需要
cap production deploy

lib/capistrano/tasks
rake文件 定义capistrano的tasks。

添加Rails master key

Capistrano::Rails 写在README里,但基本是必须步骤。
如本地尚未生成master key 执行rails secret or rake secret

编辑Capfile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
append :linked_files, "config/master.key"

namespace :deploy do
namespace :check do
before :linked_files, :set_master_key do
on roles(:app), in: :sequence, wait: 10 do
unless test("[ -f #{shared_path}/config/master.key ]")
upload! 'config/master.key', "#{shared_path}/config/master.key"
end
end
end
end
end

migration

Capistrano::Rails Recommendations 提到

1
2
set :migration_role, :app
# Defaults to :db role

这样当你的server 承担app role的时候,cap deploy:migrate 会起作用。
或者在config/deploy/your_env.rb roles 里添加db 角色。

其他一些总结的任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

def package_add(*pkgs)
pkgs.each do |pkg|
unless test "rpm -qa #{pkg}"
execute("yum -y install #{pkg}")
end
end
end

namespace :rbenv do
before :update_ruby_build,:dep do
on roles(:app) do
execute("yum install -y openssl-devel readline-devel zlib-devel")
end
end
end

namespace :puma do
before :nginx_config,:install do
on roles(:app) do
package_add "nginx"

unless test "[ -d /etc/nginx/sites-available ]"
execute("mkdir -p /etc/nginx/sites-available")
end
unless test "[ -d /etc/nginx/sites-enabled ]"
execute("mkdir -p /etc/nginx/sites-enabled")
end

end
end
end

namespace :bundler do
before :install,:dep do
on roles(:app) do
package_add "epel-release","yum-utils"
package_add "postgresql-devel"
unless test "rpm -qa postgresql12-server"
execute("yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm")
execute("yum-config-manager --enable pgdg12")
execute("yum -y install postgresql12-server postgresql12")
execute("/usr/pgsql-12/bin/postgresql-12-setup initdb")
execute("systemctl enable --now postgresql-12")
execute("systemctl status postgresql-12")
execute("firewall-cmd --add-service=postgresql --permanent")
execute("firewall-cmd --reload")
# execute("sudo su - postgres;psql -c 'alter user postgres with password \'\'' ")
end
package_add "sqlite-devel"

# execute("yum -y install nodejs")

end
end
end
Rails log过滤
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×