Capistrano 3 change ssh_options inside task
Capistrano 3
I had hard time finding out a solution. But the solution is much nicer than version 2. Cap team has done a great job. Make sure you have updated Capistrano to 3.2.x+ Here's the trick:
# 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
Make sure both 'deploy' & 'foo' user can ssh into the box. Within your tasks, use the on
keyword:
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
Other gotchas:
Make sure some Capistrano tasks skips the additional no release role you added. Otherwise, it might cause file permission issues during deployment. E.g. capistrano/bundler
extension need to override the default bundler_roles
set :bundler_roles, %w(web app db) # excludes the no release role.
- Read more about no_release flag: related Github issue.
Capistrano 2
I used to have a custom functions to close and reconnect ssh sessions.
- Reference Paul Gross's Blog
Place the following methods in deploy.rb
. Call with_user
to switch ssh session. Slightly simplified version:
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
Usage:
task :update_nginx_config, :roles => :app do
with_user "root" do
sudo "nginx -s reload"
end
end
Declaring and using ruby functions within capistrano 3 tasks
There are two problems with your example, the correct lines are:
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }
You can see the complete example here
Capistrano Deploying to Wrong Enviornment
I was able to resolve my problem. My problem was occurring because I forgot to add my production secret_key_base as an environment variable in my production server.
Symfony2 deployment to production using Capistrano 3
The folders structure looks like this:
├── current -> /home/myusername/mydomain/releases/20150120114500/
├── releases
│ ├── 20150080072500
│ ├── 20150090083000
│ ├── 20150100093500
│ ├── 20150110104000
│ └── 20150120114500
├── repo
│ └── <VCS related data>
├── revisions.log
└── shared
└── <linked_files and linked_dirs>
From documentation:
current is a symlink pointing to the latest release. This symlink is updated at the end of a successful deployment. If the deployment fails in any step the current symlink still points to the old release.
Your main app.php
file is located in /home/myusername/mydomain/current/web/app.php
and you should point your domain exactly to that directory, otherwise you must add directories in path to access that file (eg. mydomain.com/myusername/mydomain/current/web
).
Just point your domain to /home/myusername/mydomain/current/web/
.
Rails deployment to staging using capistrano generates the following error
There are two possible solutions
Copy your private key to your deployment machine
As you can push to Github (this is what I understand) you have a private key on your development machine and the corresponding public key on Github. You can copy your private key onto your deployment machine to get rid of the errordeveleopment-machine$ scp ~/.ssh/id-rsa.pub deployment-machine:key
Then ssh to your deployment machine and do
deployment-machine$ mv ~/.ssh/id-rsa.pub ~/.ssh/id-rsa.old
deployment-machine$ mv ~/key ~/.ssh/id-rsa.pub
- Create a key on your deployment machine and add public key to Github
There is a easy to follow explanation how to do that at Github Help
Hope this helps.
Links to projects using Capistrano
If you are on Rails 3:
- capfile
- deploy.rb
- environments
- development notebook
If you are on Rails 4:
- capfile
- deploy.rb
- environments
- development notebook
Why I must install gems manually after deploy using Capistrano
You need to specify the rvm ruby version & gemset in your deploy.rb
as follow:
set :rvm_ruby_version, '2.0.xx@mygemset'
Related Topics
Using Compass from Ruby (Not Shell)
What Is Difference Between "P" and "Pp"
How to Memoize a Method That May Return True, False, or Nil in Ruby
Ruby on Rails Multiple Http Request at the Same Time
Rails 3 - How to Set Start Date to Date_Select Method
How to Marshal a Lambda (Proc) in Ruby
Delete Contents of Array Based on a Set of Indexes
Add a CSS Class to <%= F.Submit %>
How to Upload a Text File and Parse Contents into Database in Ror
How to Convert Any Method to Infix Operator in Ruby
Splitting Routes File into Multiple Files
Modifying the Default Hash Value
Rails 3, Displaying Jpg Images from the Local File System Above Rails_Root
How to Ensure Ruby Gems Are Installed in Right Place to Be Executed by Bundler
Is There a Best Directory to Place Image Uploads on Heroku