# frozen_string_literal: true
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# Load the SCM plugin appropriate to your project:
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Include tasks from other gems included in your Gemfile
require "capistrano/rvm"
require "capistrano/bundler"
require "capistrano/rails"
require "capistrano/rails/console"
require "capistrano/rake"
require "capistrano/puma"
install_plugin Capistrano::Puma, load_hooks: false # Default puma tasks
install_plugin Capistrano::Puma::Workers, load_hooks: false
set :puma_init_active_record, true
# Add database AND assets tasks to capistrano to a Rails project
# Read more: https://github.com/sgruhier/capistrano-db-tasks#capistranodbtasks
require "capistrano-db-tasks"
# if you want to remove the local dump file after loading
set :db_local_clean, true
# if you want to remove the dump file from the server after downloading
set :db_remote_clean, true
# if you are highly paranoid and want to prevent any push operation to the server
set :disallow_pushing, true
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.2"
set :application, "rails-deployment-demo"
set :repo_url, "git@github.com:zhaqiang/rails-deployment-demo.git"
set :deploy_to, "/var/www/rails-deployment-demo"
set :init_system, :systemd
# Default value for :linked_files is []
append :linked_files, "config/database.yml", "config/master.key"
# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
namespace :puma do
desc "Create Directories for Puma Pids and Socket"
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Initialize configuration using example files provided in the distribution"
task :upload_config do
on release_roles :all do |host|
Dir["config/master.key", "config/*.yml.example"].each do |file|
save_to = "#{shared_path}/config/#{File.basename(file, '.example')}"
unless test "[ -f #{save_to} ]"
upload!(File.expand_path(file), save_to)
end
end
end
end
before "deploy:check:linked_files", "deploy:upload_config"
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
after :finishing, :cleanup
after :finishing, :restart
end
配置 production.rb 文件
修改 config/deploy/production.rb 文件,根据实际情况替换 app_url 和 IP 地址为你服务器的值。
# frozen_string_literal: true
set :branch, "master"
set :rails_env, "production"
set :app_url, "https://deploy-demo.zq-dev.com"
set :rvm_ruby_version, "2.6.5"
set :rvm_custom_path, "/usr/share/rvm"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{shared_path}/log/puma.error.log"
set :puma_error_log, "#{shared_path}/log/puma.access.log"
server "your-server-ip", user: "deploy", roles: %w[app db web]
$ sudo -u postgres psql
postgres=# create database rails_deployment_demo_production;
postgres=# create user deploy with encrypted password 'your-password';
postgres=# grant all privileges on database rails_deployment_demo_production to deploy;
postgres=# \q
检查本地的 config/database.yml,确保 production 块的数据库连接配置在跟服务器上的匹配。