這兩天正在學習 ruby on rails 的項目部署,手裏有臺國外的vps搭我的網站,系統是 centos7,因而去網上查了一些資料怎麼作項目部署。大多文章都推薦使用 Nginx + passenger + mina 的方式。
Mina是一個自動部署的腳本,有點像日常項目上線的一鍵上線的操做,基於git
將本地開發好的項目發佈到線上,自動備份不一樣發佈版本,自動將當前版本指向最新版本,對於上線回滾挺有用的。廢話很少說,網上相關的文章能查到不少資料,我照着網上提供的 mina 腳本進行部署,結果出現各類錯誤,主要是提示變量名不對之類。
這個時候仍是看Mina官方文檔最靠譜。html
在gem文件里加入 mina:git
# Gemfile gem 'mina'
運行 bundle install
,進行安裝。github
運行 mina init
,生成初始化配置文件 config/deploy.rb
要運行的任務修改這個配置文件就能夠。centos
set :application_name, 'demo' set :domain, 'root@servername' set :deploy_to, '/root/wwwroot/deploy' set :repository, 'git@github.com:virola/ror-events.git' set :branch, 'master' set :shared_paths, ['config/database.yml', 'config/application.yml', 'log', 'tmp/sockets', 'tmp/pids', 'public/uploads']
shared_paths
這個變量,列出的就是要共享的文件和目錄,部署後會軟鏈到 current 中。ruby
服務器部署的目錄結構應該是這樣:服務器
/root/wwwroot/deploy/ # The deploy_to path |- releases/ # Holds releases, one subdir per release | |- 1/ | |- 2/ | |- 3/ | '- ... |- shared/ # Holds files shared between releases | |- logs/ # Log files are usually stored here | `- ... '- current/ # A symlink to the current release in releases/
由於初始時服務器上尚未創建對應的目錄結構和文件,這時咱們須要先定義一個 setup
task。app
mina使用的任務是符合 rake
的 task ,以前我對rake瞭解很少,因此這裏使用網上給出的配置時出現了不少問題。經過 mina setup -v
能夠查看具體運行信息,發現其中使用了 #{deploy_to}
這樣的變量彷佛不能被識別,去查官方給的 task example,發現他們用的方式是 command
命令和 #{fetch(:deploy_to)}
,因而所有改爲這個變量試了一下,居然就成功了。。。dom
task :setup do # 在服務器項目目錄的shared中建立log文件夾 command %{mkdir -p "#{fetch(:shared_path)}/log"} command %{chmod g+rx,u+rwx "#{fetch(:shared_path)}/log"} # 在服務器項目目錄的shared中建立config文件夾 下同 command %{mkdir -p "#{fetch(:shared_path)}/config"} command %{chmod g+rx,u+rwx "#{fetch(:shared_path)}/config"} command %{touch "#{fetch(:shared_path)}/config/database.yml"} command %{touch "#{fetch(:shared_path)}/config/secrets.yml"} # puma.rb 配置puma必須得文件夾及文件 command %{mkdir -p "#{fetch(:deploy_to)}/shared/tmp/pids"} command %{chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/tmp/pids"} command %{mkdir -p "#{fetch(:deploy_to)}/shared/tmp/sockets"} command %{chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/tmp/sockets"} command %{touch "#{fetch(:deploy_to)}/shared/config/puma.rb"} comment %{-----> Be sure to edit 'shared/config/puma.rb'.} # tmp/sockets/puma.state command %{touch "#{fetch(:deploy_to)}/shared/tmp/sockets/puma.state"} comment %{-----> Be sure to edit 'shared/tmp/sockets/puma.state'.} # log/puma.stdout.log command %{touch "#{fetch(:deploy_to)}/shared/log/puma.stdout.log"} comment %{-----> Be sure to edit 'shared/log/puma.stdout.log'.} # log/puma.stdout.log command %{touch "#{fetch(:deploy_to)}/shared/log/puma.stderr.log"} comment %{-----> Be sure to edit 'shared/log/puma.stderr.log'.} comment %{-----> Be sure to edit '#{fetch(:shared_path)}/config/database.yml'.} end
以後執行socket
mina setup
能夠看到成功運行的信息。ruby-on-rails
另外 mina deploy 的腳本和網上給的配置基本一致。就是注意沒有 mina setup 以前運行 mina deploy 會提示目錄不存在而中斷,因此必須先配好 setup task 才行。
task :deploy do # uncomment this line to make sure you pushed your local branch to the remote origin # invoke :'git:ensure_pushed' 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' invoke :'deploy:cleanup' on :launch do in_path(fetch(:current_path)) do command %{mkdir -p tmp/} command %{touch tmp/restart.txt} end end on :clean do command %{log "failed deployment"} end end # you can use `run :local` to run tasks on local machine before of after the deploy scripts # run(:local){ say 'done' } end