以前都是手动部署,特别麻烦,而且非常的累。现在尝试自动部署方案。
使用的自动部署方案是:capistrano
安装
在Rails项目执行
以后每次更新,使用
参考文章
https://github.com/capistrano/capistrano/
ruby-china.org/topics/2020
http://ihower.tw/rails3/deployment.html
使用的自动部署方案是:capistrano
安装
gem install capistrano
在Rails项目执行
capify .
写配置文件
部署使用的配置文件是 config/deploy.rb
如下两篇文件是可以参考的
require 'bundler/capistrano'
# require 'hoptoad_notifier/capistrano'
set :application, "your-project-name"
set :branch, "master"
set :repository, "git@github.com:ihower/your-project-name.git"
set :scm, "git"
set :user, "rails" # 一個伺服器上的帳戶用來放你的應用程式,不需要有sudo權限,但是需要有權限可以讀取Git repository拿到原始碼
set :port, "22"
set :deploy_to, "/home/rails"
set :deploy_via, :remote_cache
set :use_sudo, false
role :web, "111.222.333.444"
role :app, "111.222.333.444"
role :db, "111.222.333.444", :primary => true
namespace :deploy do
task :copy_config_files, :roles => [:app] do
db_config = "#{shared_path}/database.yml"
run "cp #{db_config} #{release_path}/config/database.yml"
end
task :update_symlink do
run "ln -s {shared_path}/public/system {current_path}/public/system"
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
# after "deploy:update_code", "deploy:copy_config_files" # 如果將database.yml放在shared下,請打開
# after "deploy:finalize_update", "deploy:update_symlink" # 如果有實作使用者上傳檔案到public/system,請打開
# encoding: utf-8
require 'bundler/capistrano' #添加之后部署时会调用bundle install, 如果不需要就可以注释掉
set :keep_releases, 10 #只保留10个备份
set :application, "myapp" #应用名称
set :repository, "git@myapp.com:/data/gitrepo/myapp"
set :deploy_to, "/data/www"
set :current_public, "/data/www/current/public"
set :shared_path, "/data/www/shared"
set :user, "deploy" #登录部署机器(myapp.com)的用户名
set :password, "123456" #登录部署机器的密码, 如果不设部署时需要输入密码
set :use_sudo, false #执行的命令中含有sudo, 如果设为false, 用户所有操作都有权限
set :scm, :git
set :bundle_flags, '--quiet'
set :copy_exclude, [".git", "spec"]
set :group_writable, false
role :web, "192.168.0.106" #Your HTTP server, Apache/etc
role :app, "192.168.0.106" #This may be the same as your `Web` server
role :db, "192.168.0.106", :primary => true #This is where Rails migrations will run
default_run_options[:pty] = true #pty: 伪登录设备
#after, before 表示在特定操作之后或之前执行其他任务
after "deploy:update", "deploy:migrate"
after "deploy:migrate", "deploy:symbol_resource"
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
desc "restart the paozhoumo application"
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
# rails 3.1首次访问时间较长,发布成功后先访问一下,进行初始化
run "curl -s -o /dev/null 127.0.0.1"
end
#用新的拉下来的public里面的内容替换旧的shared目录的,然后删除掉public,用shared链接到public
desc "copy everyting to overwrite the shared public folder"
task :symbol_resource do
#拷贝新拉下来的images/css/jslib,以及目录下的文件
run "rm -rf #{shared_path}/images/*"
run "cp -r #{current_public}/images/* #{shared_path}/images/"
run "rm -rf #{shared_path}/css/*"
run "cp -r #{current_public}/css/* #{shared_path}/css/"
run "rm -rf #{shared_path}/jslib/*"
run "cp -r #{current_public}/jslib/* #{shared_path}/jslib/"
run "rm -f #{shared_path}/*.*"
run "cp #{current_public}/*.* #{shared_path}"
#然后把拉下来的public目录删除掉
run "rm -rf #{current_public}"
#把共享的share文件夹中
run "ln -sf #{shared_path} #{current_public}"
end
end
cap deploy:setup
cap deploy:cold
以后每次更新,使用
cap deploy
参考文章
https://github.com/capistrano/capistrano/
ruby-china.org/topics/2020
http://ihower.tw/rails3/deployment.html