我正在使用带有Capistrano的Rails 4部署在Ubuntu 14.04服务器上,我的用户可以上传存储在/public/uploads
中的文件。应始终保留此文件夹以用于新版本,并且永远不应从git存储库获取文件。
在阅读了几个指南后,我按照这些步骤进行了操作:
将此添加到我的deploy.rb
:
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
将此添加到我的/.gitignore
:
# Ignore uploads
/public/uploads
删除当前git存储库中的文件夹:
git rm -r --cached public/uploads
现在,如果我运行cap deploy:setup
,我会收到此错误:
Stage not set, please call something such as `cap production deploy`, where production is a stage you have defined.
如果我定义阶段production
部署开始,但我很早就中止了:
cap aborted!
Don't know how to build task 'deploy:setup' (see --tasks)
如果我使用常规命令部署cap production deploy
,一切正常,但public/uploads/
文件夹将在更新之前更新。
不确定我是否只是错过了设置命令,或者我是否在设置时犯了错误。我的deploy.rb
看起来像这样:
# Change these
server 'ip', port: port, roles: [:web, :app, :db], primary: true
set :repo_url, 'git@github.com:blabla/bla.git'
set :application, 'blub'
set :user, 'blib'
set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{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.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
#set :linked_files, %w{config/database.yml}
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 "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/master`
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
namespace :sake do
desc "Run a task on a remote server."
# run like: cap staging sake:invoke task="a_certain_task"
task :invoke do
on roles(:all) do |h|
execute "cd #{fetch(:deploy_to)}/current && bundle exec rake #{ENV['task']} RAILS_ENV=#{fetch(:rails_env)}"
end
end
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma