# frozen_string_literal: true# Load DSL and set up stagesrequire"capistrano/setup"# Include default deployment tasksrequire"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 Gemfilerequire"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 tasksinstall_plugin Capistrano::Puma::Workers, load_hooks: falseset :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#capistranodbtasksrequire"capistrano-db-tasks"# if you want to remove the local dump file after loadingset :db_local_clean,true# if you want to remove the dump file from the server after downloadingset :db_remote_clean,true# if you are highly paranoid and want to prevent any push operation to the serverset :disallow_pushing,true# Load custom tasks from `lib/capistrano/tasks` if you have any definedDir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
# config valid for current version and patch releases of Capistranolock "~> 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"endend before :start, :make_dirsendnamespace :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')}"unlesstest"[ -f #{save_to} ]" upload!(File.expand_path(file), save_to)endendendend 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"endend desc "Restart application" task :restart do on roles(:app), in: :sequence, wait: 5do invoke "puma:restart"endend after :finishing, :cleanup after :finishing, :restartend
配置 production.rb 文件
修改 config/deploy/production.rb 文件,根据实际情况替换 app_url 和 IP 地址为你服务器的值。
$ 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 块的数据库连接配置在跟服务器上的匹配。