capistrano 3.0.1使って非railsアプリをサーバ上に配置してみた。
capistrano2でやってみた記事はこちら capistrano2で非railsを配置してみる - Qiita [キータ]
試してみた手順
- capistrano初期化
$ mkdir test
$ cd test
$ bundle init
$ echo 'gem "capistrano"' >> Gemfile
$ bundle install --path vendor/bundle
$ bundle exec cap install
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
Capified
$ tree -L 3
.
├── Capfile
├── Gemfile
├── Gemfile.lock
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
├── lib
│ └── capistrano
│ └── tasks
└── vendor
└── bundle
└── ruby
-
config/deploy/staging.rbで接続サーバを設定
- コメントアウトしてあるところを参照
-
config/deploy.rbを設定
- 一部コメントアウトを外して設定
set :application, 'MyTips'
set :repo_url, 'https://github.com/mechamogera/MyTips'
set :scm, :git
set :deploy_to, '/var/www/my_app'
set :keep_releases, 5
...
- EC2(AmazonLinux)を作成
- 以下のように初期設定
# EC2上
$ sudo yum install git httpd -y
$ sudo mkdir /var/www/my_app
$ sudo chown ec2-user:ec2-user /var/www/my_app
- capistrano実行
$ bundle exec cap staging deploy
- EC2側確認
- current:現在のリリースへのシンボルリンク
- releases/[日時]:リリース毎にリポジトリーのミラーから作成されるgit archiveの解凍ディレクトリ
- repo:リポジトリのミラー(ディレクトリなければclone --mirror、あればgit remote updateかな)
- share:各リリース間で共通なファイルが配置され各リリースにリンクされるはず
$ tree -L 2 /var/www/my_app/
/var/www/my_app/
├── current -> /var/www/my_app/releases/20131127025659
├── releases
│ └── 20131127025659
├── repo
│ ├── branches
│ ├── config
│ ├── description
│ ├── FETCH_HEAD
│ ├── HEAD
│ ├── hooks
│ ├── info
│ ├── objects
│ ├── packed-refs
│ └── refs
├── revisions.log
└── shared
capistranoのフローメモ
-
Flow - Capistrano
- 複数サーバーを指定した場合各フロー毎に各サーバーに処理を実施
- 1つでも処理に失敗すると即停止
- deploy:starting 初期化
- deploy:started started hook
- deploy:updating releaseを取得して配置
- deploy:updated updated hook
- deploy:publishing currentのシンボルリンクを更新
- deploy:published published hook
- deploy:finishing 余分なreleaseを削除
- deploy:finished finished hook