卡皮斯特拉诺 3
我很难找到解决方案。但是解决方案比第 2 版要好得多。Cap 团队做得很好。确保您已将 Capistrano 更新到 3.2.x+ 这是诀窍:
# in config/deploy/production.rb, or config/deploy/staging.rb
# These roles are used for deployment that works with Cap hooks
role :app, %w{deploy@myserver.com}
role :web, %w{deploy@myserver.com}
role :db, %w{deploy@myserver.com}
# Use additional roles to run side job out side Capistrano hooks
# 'foo' is another ssh user for none-release purpose tasks (mostly root tasks).
# e.g. user 'deploy' does not have root permission, but 'foo' has root permission.
# 'no_release' flag is important to flag this user will skip some standard hooks
# (e.g. scm/git/svn checkout )
role :foo_role, %w{foo@myserver.com}, no_release: true
确保 'deploy' 和 'foo' 用户都可以 ssh 进入盒子。在您的任务中,使用on
关键字:
task :restart do
on roles(:foo_role) do
sudo "service nginx restart"
end
end
task :other_standard_deployment_tasks do
on release_roles(:all) do
# ...
end
end
其他陷阱:
确保某些 Capistrano 任务跳过您添加的额外的无释放角色。否则,可能会在部署过程中导致文件权限问题。例如capistrano/bundler
扩展需要覆盖默认值bundler_roles
set :bundler_roles, %w(web app db) # excludes the no release role.
卡皮斯特拉诺 2
我曾经有一个自定义函数来关闭和重新连接 ssh 会话。
将以下方法放入deploy.rb
. 调用with_user
切换 ssh 会话。稍微简化的版本:
def with_user(new_user, &block)
old_user = user
set :user, new_user
close_sessions
yield
set :user, old_user
close_sessions
end
def close_sessions
sessions.values.each { |session| session.close }
sessions.clear
end
用法:
task :update_nginx_config, :roles => :app do
with_user "root" do
sudo "nginx -s reload"
end
end