这两天正在学习 ruby on rails 的项目部署,手里有台国外的vps搭个人网站,系统是 centos7,于是去网上查了一些资料怎么做项目部署。大多文章都推荐使用 Nginx + passenger + mina 的方式。 Mina是一个自动部署的脚本,有点像平常项目上线的一键上线的操作,基于git将本地开发好的项目发布到线上,自动备份不同发布版本,自动将当前版本指向最新版本,对于上线回滚挺有用的。废话不多说,网上相关的文章能查到很多资料,我照着网上提供的 mina 脚本进行部署,结果出现各种错误,主要是提示变量名不对之类。 这个时候还是看Mina官方文档最靠谱。
安装
在gem文件里加入 mina:
1 2
# Gemfile gem 'mina'
运行 bundle install ,进行安装。
配置
运行 mina init ,生成初始化配置文件 config/deploy.rb 要运行的任务修改这个配置文件就可以。
配置好基本参数
1 2 3 4 5 6 7 8 9 10
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, ['log', 'tmp/sockets', 'tmp/pids', 'public/uploads'] set :shared_files, ['config/database.yml', 'config/secrets.yml']
/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/
后来查看文档,发现已经默认配置好了 mina setup ,只要直接运行就可以自动生成项目部署结构,根本不需要自定义,结果我还纠错研究了半天(- -!)。
mina deploy
另外 mina deploy 的脚本和网上给的配置基本一致。就是注意没有 mina setup 之前运行 mina deploy 会提示目录不存在而中断,所以必须先配好 setup task 才行。
task :deploydo # 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 :launchdo in_path(fetch(:current_path)) do command %{mkdir -p tmp/} command %{touch tmp/restart.txt} end end
on :cleando 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