如何使用 Capistrano Deploy 文件夹
How to use the Capistrano Deploy folder
Rails 5.2.3
Capistrano 3.11
我正在学习如何使用 Capistrano,但在这个阶段,不确定如何使用部署文件夹。当我安装 capistrano-rails gem 时,它创建了几个东西:
config/deploy 文件夹,包含 production.rb 和 staging.rb 个文件
config/deploy.rb 文件
Capfile
我能否将 config/deploy.rb 文件保留为空,并将脚本放入 config/deploy 下的各个文件中(生产、暂存等),然后假设我这样做时:
cap staging deploy
并假设它将使用 deploy/staging.rb 文件?
deploy.rb
包含部署脚本的通用配置。像
puma 配置、repo url、sidekiq 等
例如
set :repo_url, 'repo_url.git'
set :application, 'app_name'
set :user, 'app'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, true
set :deploy_via, :remote_cache
set :deploy_to, "/var/deploy/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log, "#{release_path}/log/puma.error.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :bundle_flags, '--deployment'
set :sidekiq_config, -> { File.join(shared_path, 'config', 'sidekiq.yml') }
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# append :linked_files, "config/database.yml", "config/secrets.yml", ".env"
set :linked_files, %w{.env}
# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system", "public/uploads", "vendor/bundle", "bundle"
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
config/deploy/staging.rb
此文件包含环境和服务器特定配置。
server '1.2.1.255', user: 'ubuntu', roles: [:web, :app, :db], primary: true, ssh_options: { forward_agent: true }
set :rails_env, :staging
set :rack_env, :staging
set :stage, :staging
set :branch, :staging
希望对您有所帮助。
Rails 5.2.3
Capistrano 3.11
我正在学习如何使用 Capistrano,但在这个阶段,不确定如何使用部署文件夹。当我安装 capistrano-rails gem 时,它创建了几个东西:
config/deploy 文件夹,包含 production.rb 和 staging.rb 个文件
config/deploy.rb 文件
Capfile
我能否将 config/deploy.rb 文件保留为空,并将脚本放入 config/deploy 下的各个文件中(生产、暂存等),然后假设我这样做时:
cap staging deploy
并假设它将使用 deploy/staging.rb 文件?
deploy.rb
包含部署脚本的通用配置。像 puma 配置、repo url、sidekiq 等
例如
set :repo_url, 'repo_url.git'
set :application, 'app_name'
set :user, 'app'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, true
set :deploy_via, :remote_cache
set :deploy_to, "/var/deploy/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log, "#{release_path}/log/puma.error.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :bundle_flags, '--deployment'
set :sidekiq_config, -> { File.join(shared_path, 'config', 'sidekiq.yml') }
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# append :linked_files, "config/database.yml", "config/secrets.yml", ".env"
set :linked_files, %w{.env}
# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system", "public/uploads", "vendor/bundle", "bundle"
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
config/deploy/staging.rb
此文件包含环境和服务器特定配置。
server '1.2.1.255', user: 'ubuntu', roles: [:web, :app, :db], primary: true, ssh_options: { forward_agent: true }
set :rails_env, :staging
set :rack_env, :staging
set :stage, :staging
set :branch, :staging
希望对您有所帮助。