使用ruby部署工具mina快速部署nodejs應用教程_ruby專題

來源:互聯網
上載者:User

前面有一篇文章講到過用git的hook部署應用,hook的方法有一個缺陷就是每次都要到伺服器去修改一下hook對應的設定檔,這個設定檔是與當前倉庫分離的,調試上會有一些麻煩,藉助ruby的一個部署工具mina可以快速的在伺服器部署nodejs應用。

安裝mina

複製代碼 代碼如下:

gem install mina

安裝之後,它需要一個設定檔,預設情況下是目前的目錄的config/deploy.rb

簡單的配置

複製代碼 代碼如下:

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之前一般需要準備一些目錄,可以通過 mina setup來設定,預設情況下,它會在指定的伺服器上建立下面的目錄結構

複製代碼 代碼如下:

.
├── releases 發布的版本
└── shared 這裡可以放公用的檔案,比如node_modules

運行mina deploy它會執行task deploy裡指定的命令,比如上面的會進行:

1.登入到伺服器
2.git clone 到scm目錄
3.在tmp目錄裡建立一個build-xxxxx的目錄,然後開始執行bundle install
4.在releases裡建立一個發布版本號碼目錄,移動build-xxxxx裡的內容進去
5.軟連結current到剛才的版本號碼目錄

nodejs應用的發布樣本

複製代碼 代碼如下:

require 'mina/git'

set :term_mode, nil
# 這裡一個虛擬機器的ip
set :domain, '192.168.56.101'
# 登入到機器的使用者名稱
set :user, 'test' # Username in the server to SSH to.
# 發布的目錄
set :deploy_to, '/home/test/doitnow'
# 發布的git倉庫地址
set :repository, 'ssh://jb51.net@192.168.56.1/Users/jb51.net/works/doitnow'
# 發布的git分支
set :branch, 'master'

# 設定需要軟連結的目錄
# 軟連結node_modules,可以防止每次npm install時花費的大量時間
set :shared_paths, ['log', 'tmp', 'node_modules']

# 這裡使用forever來管理node進程,也推薦使用pm2
set :forever,"#{deploy_to}/shared/node_modules/forever/bin/forever"

# 初始化的時候建立目錄,分配目錄許可權
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'
    # 安裝模組
    # 靜態資源的編譯可以放到package.json裡的{scripts:{install:'xxxxx'}}
    queue  "npm install"

    to :launch do
      # 重啟應用
      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

來源:http://jser.me

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.