前面有一篇文章講到過用git的hook部署應(yīng)用,hook的方法有一個(gè)缺陷就是每次都要到服務(wù)器去修改一下hook對(duì)應(yīng)的配置文件,這個(gè)配置文件是與當(dāng)前倉庫分離的,調(diào)試上會(huì)有一些麻煩,借助ruby的一個(gè)部署工具mina可以快速的在服務(wù)器部署nodejs應(yīng)用。
require 'mina/git'
require 'mina/bundler'
set :domain, 'your.server.com'
set :user, 'flipstack'
set :repository, 'flipstack'
task :deploy do
deploy do
# Preparations here
invoke :'git:clone'
invoke :'bundle:install'
end
end
task :restart do
queue 'sudo service restart apache'
end
在正式的deploy之前一般需要準(zhǔn)備一些目錄,可以通過 mina setup來設(shè)置,默認(rèn)情況下,它會(huì)在指定的服務(wù)器上創(chuàng)建下面的目錄結(jié)構(gòu)
require 'mina/git'
set :term_mode, nil
# 這里一個(gè)虛擬機(jī)的ip
set :domain, '192.168.56.101'
# 登錄到機(jī)器的用戶名
set :user, 'test' # Username in the server to SSH to.
# 發(fā)布的目錄
set :deploy_to, '/home/test/doitnow'
# 發(fā)布的git倉庫地址
set :repository, 'ssh://jb51.net@192.168.56.1/Users/jb51.net/works/doitnow'
# 發(fā)布的git分支
set :branch, 'master'
# 設(shè)置需要軟鏈接的目錄
# 軟鏈接node_modules,可以防止每次npm install時(shí)花費(fèi)的大量時(shí)間
set :shared_paths, ['log', 'tmp', 'node_modules']
# 這里使用forever來管理node進(jìn)程,也推薦使用pm2
set :forever,"#{deploy_to}/shared/node_modules/forever/bin/forever"
# 初始化的時(shí)候創(chuàng)建目錄,分配目錄權(quán)限
task :setup do
queue "mkdir -p #{deploy_to}/shared/log"
queue "chmod g+rx,u+rwx #{deploy_to}/shared/log"
queue "mkdir -p #{deploy_to}/shared/node_modules"
queue "chmod g+rx,u+rwx #{deploy_to}/shared/node_modules"
end
desc "Deploys the current version to the server."
task :deploy do
deploy do
invoke :'git:clone'
# 鏈接目錄
invoke :'deploy:link_shared_paths'
# 安裝模塊
# 靜態(tài)資源的編譯可以放到package.json里的{scripts:{install:'xxxxx'}}
queue "npm install"
to :launch do
# 重啟應(yīng)用
queue "NODE_ENV=production #{forever} stopall"
# 注意把log放到shared里去
queue "NODE_ENV=production #{forever} start -o #{deploy_to}/shared/log/output.log -e #{deploy_to}/shared/log/error.log -a app.js "
end
end
end