每次重新部署,public裡面的圖片好像會清空,怎辦?
將public/uploads
加入:shared_paths
的清單內:
set :shared_paths, ['config/database.yml', 'log', 'config/config.yml', 'config/secrets.yml', 'public/uploads']
再將以下程式碼加入到:environment
queue! %[mkdir -p "#{deploy_to}/shared/public/uploads"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/public/uploads"]
但如果要手動清空 /public的資料
task :clean do queue! %[cd "#{deploy_to}/shared/public/uploads"] queue 'rm -rf *' end
然後下指令 mina clean
就可以手動清空。
以下是完整版本:
require 'mina/bundler' require 'mina/rails' require 'mina/git' require 'mina/rbenv' # for rbenv support. (http://rbenv.org) set :domain, 'information.coolsea.net' set :deploy_to, '/home/apps/my_project/information' set :repository, 'git@github.com:coolsea/information.git' set :branch, 'master' # Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. # They will be linked in the 'deploy:link_shared_paths' step. set :shared_paths, ['config/database.yml', 'log', 'config/config.yml', 'config/secrets.yml', 'public/uploads'] # Optional settings: set :user, 'apps' # Username in the server to SSH to. task :environment do # If you're using rbenv, use this to load the rbenv environment. # Be sure to commit your .rbenv-version to your repository. invoke :'rbenv:load' # For those using RVM, use this to load an RVM version@gemset. # invoke :'rvm:use[ruby-1.9.3-p125@default]' end ########################################################################## # # Create new host tasks # Tasks below are related to deploying a new version of the application # ########################################################################## # Function extracted from http://blog.nicolai86.eu/posts/2013-05-06/syncing-database-content-down-with-mina # allowing to read the content of the database.yml file RYAML = <<-BASH function ryaml { ruby -ryaml -e 'puts ARGV[1..-1].inject(YAML.load(File.read(ARGV[0]))) {|acc, key| acc[key] }' "$@" }; BASH # Put any custom mkdir's in here for when `mina setup` is ran. # For Rails apps, we'll make some of the shared paths that are shared between # all releases. task :setup => :environment do queue! %[mkdir -p "#{deploy_to}/shared/log"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"] queue! %[mkdir -p "#{deploy_to}/shared/config"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"] queue! %[mkdir -p "#{deploy_to}/shared/public/uploads"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/public/uploads"] queue! %[touch "#{deploy_to}/shared/config/database.yml"] queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."] invoke :'setup:db:database_yml' end # Populate file database.yml with the appropriate rails_env # Database name and user name are based on convention # Password is defined by the user during setup desc "Populate database.yml" task :'setup:db:database_yml' => :environment do puts "Enter a name for the new database" db_name = STDIN.gets.chomp puts "Enter a user for the new database" db_username = STDIN.gets.chomp puts "Enter a password for the new database" db_pass = STDIN.gets.chomp # Virtual Host configuration file database_yml = <<-DATABASE.dedent #{rails_env}: adapter: mysql2 encoding: utf8 database: #{db_name} username: #{db_username} password: #{db_pass} host: localhost timeout: 5000 DATABASE queue! %{ echo "-----> Populating database.yml" echo "#{database_yml}" > #{deploy_to!}/shared/config/database.yml echo "-----> Done" } invoke :'setup:db' end # Create the new database based on information from database.yml # In this application DB, user is given full access to the new DB desc "Create new database" task :'setup:db' => :environment do queue! %{ echo "-----> Import RYAML function" #{RYAML} echo "-----> Read database.yml" USERNAME=$(ryaml #{deploy_to!}/#{shared_path!}/config/database.yml #{rails_env} username) PASSWORD=$(ryaml #{deploy_to!}/#{shared_path!}/config/database.yml #{rails_env} password) DATABASE=$(ryaml #{deploy_to!}/#{shared_path!}/config/database.yml #{rails_env} database) echo "-----> Create SQL query" Q1="CREATE DATABASE IF NOT EXISTS $DATABASE;" Q2="GRANT USAGE ON *.* TO $USERNAME@localhost IDENTIFIED BY '$PASSWORD';" Q3="GRANT ALL PRIVILEGES ON $DATABASE.* TO $USERNAME@localhost;" Q4="FLUSH PRIVILEGES;" SQL="${Q1}${Q2}${Q3}${Q4}" echo "-----> Execute SQL query to create DB and user" echo "-----> Enter MySQL root password on prompt below" #{echo_cmd %[mysql -uroot -p -e "$SQL"]} echo "-----> Done" } end desc "Deploys the current version to the server." task :deploy => :environment do deploy do # Put things that will set up an empty directory into a fully set-up # instance of your project. invoke :'git:clone' invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'rails:db_migrate' invoke :'rails:assets_precompile' end end task :restart do queue 'sudo service nginx restart' end task :clean do queue! %[cd "#{deploy_to}/shared/public/uploads"] queue 'rm -rf *' end ######################################################################### # # Libraries # ########################################################################## # # See https://github.com/cespare/ruby-dedent/blob/master/lib/dedent.rb # class String def dedent lines = split "\n" return self if lines.empty? indents = lines.map do |line| line =~ /\S/ ? (line.start_with?(" ") ? line.match(/^ +/).offset(0)[1] : 0) : nil end min_indent = indents.compact.min return self if min_indent.zero? lines.map { |line| line =~ /\S/ ? line.gsub(/^ {#{min_indent}}/, "") : line }.join "\n" end end