在部署之间存储用户数据(主题文件)

时间:2014-03-25 16:42:32

标签: git deployment ruby-on-rails-4 capistrano production

我使用Capistrano脚本部署了一个新的rails应用程序。但是我注意到,每次使用Capistrano脚本更新应用程序时,我上传到此应用程序(主题文件)的所有用户数据都会被删除。

这当然是因为该脚本使用repo中的数据覆盖整个应用程序。

但是我应该如何在rails应用程序中存储数据(例如主题文件)以避免此问题?我应该将它存储在应用程序文件夹之外,或者在这里做什么是正常的事情?有没有办法避免这个问题?

感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

我通过在我的部署scirpt(namespace :deploy do下)中添加了这个来解决这个问题:

task :link_dependencies, :roles => :app do
    run "ln -nfs #{shared_path}/public/assets/themes/ #{current_path}/public/assets/themes"
    run "ln -nfs #{shared_path}/public/uploads/themes/ #{current_path}/public/uploads/themes"
    run "ln -nfs #{shared_path}/public/uploads/photo/ #{current_path}/public/uploads/photo"
    run "ln -nfs #{shared_path}/themes/ #{current_path}/themes"
    run "ln -nfs #{shared_path}/config/aws_environment_variables.rb #{current_path}/config/aws_environment_variables.rb"
  end

在上面我添加了after :deploy, 'deploy:link_dependencies(在将更新的应用程序从git复制到current目录后运行上面的代码。

代码在共享目录和更新的应用程序之间设置符号链接,以便更新的应用程序可以获取共享信息并写入位于当前目录之外的文件夹(因此下次重新部署时不会覆盖该文件夹)< / p>

修改 完成部署文件:

require "rvm/capistrano"                  # Load RVM's capistrano plugin.
set :rvm_ruby_string, '2.1.1'        # Or whatever env you want it to run in.
set :rvm_type, :user

# Bundler support
require 'bundler/capistrano'

# Stages
require 'capistrano/ext/multistage'
set :stages, %w(production)
set :default_stage, 'production'

set :application, "imagesite"
set :scm, :git
set :repository,  "https://github.com/your_username/your_repo.git"

set :deploy_to, "/home/rails/apps/#{application}"
set :deploy_via, :remote_cache
set :copy_exclude, [ '.git' ]

set :user, "your_linux_server_username"

# if you want to clean up old releases on each deploy uncomment this:
set :keep_releases, 10
after "deploy:restart", "deploy:cleanup"
after :deploy, 'deploy:link_dependencies'

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
  task :symlink_shared do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end

  desc <<-DESC
    Creates symbolic links to configuration files and other dependencies
    after deployment.
  DESC
  task :link_dependencies, :roles => :app do
    run "ln -nfs #{shared_path}/public/assets/themes/ #{current_path}/public/assets/themes"
    run "ln -nfs #{shared_path}/public/uploads/themes/ #{current_path}/public/uploads/themes"
    run "ln -nfs #{shared_path}/public/uploads/photo/ #{current_path}/public/uploads/photo"
    run "ln -nfs #{shared_path}/themes/ #{current_path}/themes"
    run "ln -nfs #{shared_path}/config/aws_environment_variables.rb #{current_path}/config/aws_environment_variables.rb"
  end

  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

before 'bundle:install', 'deploy:symlink_shared'