I use the following deploy.rb :
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, '*****'
set :rails_env, 'production'
set :repo_url, 'admin#test.*******.***:/srv/outils/repos/*****'
set :scm, :git
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
#after 'deploy:publishing', 'deploy:restart'
end
after 'deploy:publishing', 'deploy:restart'
It correctly deploy the app but does not restart it. What should i modify to make it restart? There is no message, error or otherwise, about the restart.
In Capistrano 3, the restart task is available and will be called, but it is empty. See the Capistrano source code in capistrano/lib/capistrano/tasks/deploy.rake for the code:
[...]
task :restart
task :failed
end
If you want the restart task to actually do something, you can change the default behavior for restart by adding this code to your deploy.rb:
namespace :deploy do
task :restart do
invoke rake-restart-something-task
end
end
Where the rake-restart-something-task can be something like deploy:service:restart_apache, or some other task that you defined in the capistrano rake file.
Related
Its showing me this error when I try to deploy it on staging my rails verison is Rails 6.1.4.4 and capistrano gem version is 3.11.0
Ruby version is 3.0.4.
It breaks everytime at deploy:check:linked_dirs .
Not sure what causing it .
bundler: failed to load command: cap (/Users/invozone-guest/.rbenv/versions/3.0.4/bin/cap)
/Users/invozone-guest/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/i18n-1.12.0/lib/i18n.rb:210:in `translate': wrong number of arguments (given 2, expected 0..1) (ArgumentError)
deploy.rb file
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"
set :application, 'spree_demo'
set :repo_url, 'git#bitbucket.org:freshprep_devs/spree_demo.git'
set :branch, 'master'
set :rbenv_type, :system
set :rbenv_ruby, '3.0.4'
set :rbenv_custom_path, '/home/deploy/.rbenv/'
# Default branch is :master
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :use_sudo, true
# Default value for :pty is false
set :pty, true
task :sake do
on roles(:app), in: :sequence, wait: 5 do
within release_path do
as :deploy do
with rails_env: :staging do
execute :rake, ENV['task'], "RAILS_ENV=staging"
end
end
end
end
end
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
execute 'sudo', '/etc/init.d/spree', 'restart'
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
my staging.rb file
role :app, %w{34.95.59.175}
role :web, %w{34.95.59.175}
role :db, %w{34.95.59.175}
server '34.95.59.175', user: 'deploy', roles: %w{web app}
set :rbenv_custom_path, '/home/deploy/.rbenv'
set :deploy_to, '/home/deploy/apps/spree_demo'
I am using Capistrano gem for deploy management of my Ruby on Rails app. Its connected with AWS server, EC2, MySQL Redis. While I am putting my command "cap production deploy" or "cap staging deploy" nothing happens. I just got stuck there.
To add here my SSH key is properly added. And in my AWS security groups, only the authorized IPs are added to have the permission of deploying. My IPs are also added.
But when I add open ports 0.0.0.0/0 in all security groups it allows me to deploy. But I shouldn't add open ports for the sace of application security.
What can be the reason and how to solve these?
screenshot of my console, nothing happens
Below is my deploy.rb file:
SSHKit.config.command_map[:rake] = 'bundle exec rake'
# config valid only for current version of Capistrano
lock '3.8.1'
set :application, 'my_app'
set :repo_url, 'git#gitlab.com:_____'
set :deploy_via, :remote_cache
set :rvm_roles, [:app, :web]
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.4.0'
set :log_level, :debug
set :pty, false
set :linked_files, %w{config/application.yml config/database.yml}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/assets public/uploads}
set :keep_releases, 10
set :whenever_roles, [:web, :app,:db]
set :whenever_identifier, "#{fetch(:application)}_#{fetch(:stage)}"
namespace :deploy do
desc 'restart (upgrade) unicorn server'
task :restart do
invoke 'unicorn:restart'
end
after :finishing, 'deploy:cleanup'
after 'deploy:publishing', 'deploy:restart'
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
within release_path do
execute :rake, 'tmp:cache:clear'
end
end
end
end
namespace :delayed_job do
desc 'List of running delayed job workers'
task :list do
on roles(:all) do |host|
execute :ps, 'aux | grep delayed_job'
end
end
desc 'Stop delayed_job workers forcefully'
task :kill do
on roles(:all) do |host|
execute :kill, "-9 $(ps aux | grep delayed_job | awk '{print $2}')"
end
end
end
task :upload_secret_files do
on roles(:all) do |host|
begin
execute "mkdir -p #{shared_path}/config"
rescue
end
upload! 'config/application.yml', "#{shared_path}/config/application.yml"
upload! 'config/database.yml', "#{shared_path}/config/database.yml"
end
end
task :log do
on roles(:all) do |host|
execute "tail -f #{current_path}/log/#{fetch(:rails_env)}.log"
end
end
desc 'Invoke a rake command on the remote server'
task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|
on primary(:app) do
within current_path do
with rails_env: fetch(:rails_env) do
rake args[:command]
end
end
end
end
I was struggling with exactly the same issue and spent several hours. Didn't get what is the issue related to. It just started working after I reboot my local OS
after i try to deploy my app via capistrano to my server i get this error message:
DEBUG [605f198a] Finished in 0.084 seconds with exit status 1 (failed).
ERROR linked file /home/deploy/myrailsapp/shared/config/database.yml does not exist on xx.xxx.xx.xxx
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#xx.xxx.xx.xxx: exit
SystemExit: exit
Tasks: TOP => deploy:check:linked_files
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deploy#xx.xxx.xx.xxx: exit
my deploy.rb is:
set :deploy_to, '/home/deploy/myrailsapp'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
i tried this tut https://www.gorails.com/deploy/ubuntu/14.04, this is my first try with capistrano.
Just create /home/deploy/myrailsapp/shared/config/database.yml file manually and adjust it.
Capistrano doesn't create (or manage) configuration file out of the box. So, you should do it manually or automate use own Capistrano scripts, Puppet, Chef, Ansible tools.
As i prefer to have my files central on the deployment server, i use this task to deploy the config files from the config dir to the linked files dir on the app server.
This uses rsync, since i use capistrano-rsync to deploy.
namespace :deploy do
task :copy_config do
on release_roles :app do |role|
fetch(:linked_files).each do |linked_file|
user = role.user + "#" if role.user
hostname = role.hostname
linked_files(shared_path).each do |file|
run_locally do
execute :rsync, "config/#{file.to_s.gsub(/.*\/(.*)$/,"\\1")}", "#{user}#{hostname}:#{file.to_s.gsub(/(.*)\/[^\/]*$/, "\\1")}/"
end
end
end
end
end
end
before "deploy:check:linked_files", "deploy:copy_config"
You can upload files using the rake task.
Add the gem to your Gemfile after setting up Capistrano, preferably in the :development group:
group :development do
gem 'capistrano', require: false
gem 'capistrano-rake', require: false
end
Аdd it to your Capfile:
require 'capistrano/rake'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
Create file lib/capistrano/tasks/setup.rake and add to it:
namespace :deploy do
namespace :check do
before :linked_files, :set_database_yml do
on roles(:app), in: :sequence, wait: 10 do
upload! 'config/database.yml', "#{shared_path}/config/database.yml"
end
end
end
end
We are relatively new to using chef to deploy our applications. Currently, an odd issue we are experiencing and have yet to find a solution for relates to our bin/passenger configuration file. For some reason when the server is constructed with chef it does not exist or chef is not creating it. Maybe capistrano is not creating it... We are a bit dumbfounded by this one.
As you can see from the attached image, we know the file is not there. All of our current scripts match 4 other servers that are running successfully but for some reason this new build will not create the file. Or TBH, we are completely missing some steps. It has been some very long nights trying to get this going.
We used chef to build the server and we are using capistrano to deploy to the box
Anyone have any thoughts? Need more information? Pointers?
Our current config/deploy.rb file:
set :application, 'digest'
set :scm, :git
set :repo_url, '{omitted private repo}'
set :branch, 'experiment/cap'
set :deploy_to, '/home/apps/api'
set :deploy_via, :remote_cache
set :user, 'deploy'
set :use_sudo, false
set :rbenv_type, :system
set :rbenv_ruby, '2.1.0'
set :rbenv_path, '/opt/rbenv'
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "mkdir -p #{release_path}/tmp ; touch #{release_path}/tmp/restart.txt"
end
end
desc 'Create application symlinks'
task :symlinks do
on roles(:app), in: :sequence, wait: 5 do
execute "rm #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/database.yml #{release_path}/config/database.yml"
execute "ln -s #{shared_path}/config/secrets.yml #{release_path}/config/secrets.yml"
execute "ln -s #{shared_path}/bin/passenger #{release_path}/bin/passenger"
end
end
after :finishing, 'deploy:cleanup'
after 'deploy:updated', 'deploy:symlinks'
end
namespace :setup do
desc 'Copy the secrets.yml and database.yml files'
task config: [ 'config/secrets.yml', 'config/database.yml' ] do |t|
on roles(:all) do
execute "mkdir -p #{shared_path}/config"
t.prerequisites.each do |file|
upload! file, "#{shared_path}/config"
end
end
end
end
In our config/deploy/staging.rb file:
set :stage, :staging
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
role :app, %w{deploy#208.94.36.146}
role :web, %w{deploy#208.94.36.146}
set :rails_env, "staging"
Our staging server bin folder:
You can see the application is making it to the box with the current releases setup:
Our current application on the server:
Our current application config directory:
I'm assuming passenger isn't in the Gemfile which is would cause the binstub to not get created. Is that the issue?
I want to deploy to production an app to my local server. i'm using capistrano 3.
this is my capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
#require 'capistrano/rails/migrations'
#require 'capistrano/rails/assets'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
this is my deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'ImpresaZiliani'
set :repo_url, 'francesco#10.0.1.8:repos/impresaziliani.git'
set :branch, 'master'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/home/francesco/impresaziliani'
# Default value for :scm is :git
set :scm, :git
set :deploy_user, "francesco"
set :rails_env, "production"
set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
set :rvm_ruby_version, '2.1.1'
set :default_env, { rvm_bin_path: '~/.rvm/bin' }
SSHKit.config.command_map[:rake] = "#{fetch(:default_env)[:rvm_bin_path]}/rvm ruby-# {fetch(:rvm_ruby_version)} do bundle exec rake"
my database.yml is ok since if i run manually the migrations on the server it works, i have tried with uncommenting the line of capistrano/rails/migrations and assets but nothing changes: when i deploy it runs fine till the bundler install, then without any warning or error, skip to the asset precompiler and doesn't run migrations.
how can i fix this?
thank you
You also need to make the user deploying has the role of db, such as:
server 'you_ip_address', user: 'user_name', roles: %w{web app db}
rake db:migrate is automatic per deploy in capistrano 3
you just need to uncomment #require 'capistrano/rails/migrations' in your Capfile
Both Jude Calimbas and hiveer's answers are more accurate than the accepted answer - the migration task is run automatically as part of the deploy task.
However, their answers do not explain the problem observed. The only thing that occurs to me is that the database.yml file is not explicitly linked in the deploy.rb file. So a line like
set :linked_files, %w{config/database.yml}
would have fixed it.
I know that this is an old question but it would be interesting to know more details from the OP regarding the problem and the fix.