[Grunt & Yeoman] grunt-rsync で開発サーバへ deploy する

ローカルの vagrant 仮想マシンへデプロイしてみます。

[markdown]
> * [jedrichards/grunt-rsync](https://github.com/jedrichards/grunt-rsync)

## 準備

rsync のたびにパスワードを求められるとつらいので、ssh config の設定をしておきましょう。

> * [Vagrant 仮想マシンに接続するための ssh config 設定の方法 | deadwood](https://www.d-wood.com/blog/2014/08/22_6712.html)

## インストール

“`prettyprinted
% npm install grunt-rsync –save-dev
“`

## Gruntfile

[rsyncwrapper](https://github.com/jedrichards/rsyncwrapper#options) というパッケージに依存しているようで、オプションの書き方などはこれに準拠するよう。
[公式](https://github.com/jedrichards/grunt-rsync)と下記を参考に記述してみます。

> * [node.jsアプリのデプロイにやさしい grunt-rsync | Developers.IO](http://dev.classmethod.jp/tool/node-js-grunt-rsync-deploy/)
> * [grunt rsyncとgrunt watchを使って開発サーバーをローカルと同期する | kanonjiのブログ](http://kanonji.info/blog/2013/08/19/grunt-rsync%E3%81%A8grunt-watch%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E9%96%8B%E7%99%BA%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%82%92%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%81%A8%E5%90%8C%E6%9C%9F/)

“`ruby:Gruntfile.coffee
# grunt-rsync
rsync:
options:
compareMode: ‘checksum’
recursive: true
# exclude: [‘.git*’]
dryrun:
options:
src: ‘<%= dir.dest %>/’
dest: ‘/var/www/foo’
host: ‘default’
delete: true
dryRun: true
deploy:
options:
src: ‘<%= dir.dest %>/’
dest: ‘/var/www/foo’
host: ‘default’
delete: true
“`

まず dryrun を実行してみます。
作成されるフォルダやファイルの一覧と、実行されるコマンドを確認できます。

“`prettyprinted
% grunt rsync:dryrun
Running “rsync:dryrun” (rsync) task
rsyncing dist/ >>> /var/www/foo
building file list … done
created directory /var/www/foo
./
index.html
:
sent 4727 bytes received 734 bytes 10922.00 bytes/sec
total size is 8377163 speedup is 1534.00
Shell command was: rsync dist/ default:/var/www/foo –rsh ssh –recursive –delete –dry-run –verbose –checksum
OK
Done, without errors.
“`

実行します。

“`prettyprinted
% grunt rsync:deploy
Running “rsync:deploy” (rsync) task
rsyncing dist/ >>> /var/www/foo
Shell command was: rsync dist/ default:/var/www/foo –rsh ssh –recursive –delete –checksum
OK
Done, without errors.
“`

無事完了しました。
なお、このタイミングでエラーが起きた場合は、表示されたコマンドを確認しつつ、ターミナルから試してみると良さそうです。

“`prettyprinted
% rsync dist/ default:/vaar/www/foo –rsh ssh –recursive –delete –verbose –checksum
building file list … done
rsync: mkdir “/vaar/www/foo” failed: No such file or directory (2)
rsync error: error in file IO (code 11) at main.c(576) [receiver=3.0.6]
rsync: connection unexpectedly closed (8 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /SourceCache/rsync/rsync-42/rsync/io.c(452) [sender=2.6.9]
“`

上記の例では、存在しないパス(/vaar)を指定していました。
[/markdown]