我尝试使用不同的ssh_options在同一阶段运行capistrano v.3任务。
我的produ.rb说:
set :stage, :production
set :user, 'deploy'
set :ssh_options, { user: 'deploy' }
使用此配置,capistrano将与用户部署连接,这对其他人来说是正确的。但我需要将其连接到一个特定任务,并在服务器中配置好 an_other_user 。 然后我的食谱说:
...
tasks with original user
...
task :my_task_with_an_other_user do
set :user, 'an_other_user'
set :ssh_options, { user: 'an_other_user' }
on roles(:all) do |host|
execute :mkdir, '-p', 'mydir'
end
end
...
other tasks with original user
...
执行时:
cap production namespace:my_task_with_an_other_user
capistrano使ssh conexion与original:user“deploy”(在production.rb中声明的用户)。
如何在任务中更改用户和/或ssh_options?
答案 0 :(得分:8)
我很难找到解决方案。但是解决方案比版本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
确保'部署'和& 'foo'用户可以进入框中。在您的任务中,使用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.
我曾经有一个自定义函数来关闭和重新连接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
答案 1 :(得分:0)
@activars的答案对我没用。因为当我为一个环境定义了几个角色时 - 只部署了一个:(
所以我的解决方案是创建几个环境,例如 production.rb 和 productionroot.rb 。
productionroot的内容带有no_release = true标志 - 正如您所指定的那样:
server '146.120.89.81', user: 'root', roles: %w{foo_role}, no_release: true
之后我创建了运行
的sh脚本#/usr/bin/env bash
bundle exec cap production deploy
bundle exec cap productionroot deploy