やりたいこと
install
$ gem install capistrano
$ cap -v
Capistrano Version: 3.6.1 (Rake Version: 10.4.2)
getting start
# ディレクトリつくる
mkdir sample
cd sample
# 初期ファイル生成する
cap install
# とりあえず使わないものを消す
rm config/deploy/*
全体像
.
├── Capfile
├── config
│ ├── deploy
│ │ ├── local.rb
│ │ └── recipes
│ │ ├── hello1.rb
│ │ └── pipeline.rb
│ └── deploy.rb
├── lib
│ └── capistrano
│ └── tasks
└── log
各タスク置き場のディレクトリつくる
mkdir config/deploy/recipes
deploy.rbをかく
config/deploy.rb
lock '3.6.1'
Dir['config/deploy/recipes/*.rb'].each { |plugin| load(plugin) }
local.rbかく(local用の設定ファイルつくる)
config/deploy/local.rb
set :user, ask('user-name', nil)
set :password, ask('user-password', nil)
server '127.0.0.1', port: 22, user: fetch(:user), password: fetch(:password), roles: %w{app}
pipeline.rbをかく(パイプライン制御用)
config/deploy/recipes/pipeline.rb
task :hello do
after "hello", "hello1:echo1"
end
hello1.rbをかく(実際のタスク)
config/deploy/recipes/hello1.rb
namespace :hello1 do
task :echo1 do
on roles(:app) do
execute 'echo "hello world"'
end
end
end
実行例
localhostにsshでログインできる状態で。
$ cap local hello --trace
** Invoke local (first_time)
** Execute local
** Invoke load:defaults (first_time)
** Execute load:defaults
Please enter user-name ():
Please enter user-password ():
** Invoke hello (first_time)
** Execute hello
** Invoke hello1:echo1 (first_time)
** Execute hello1:echo1
00:00 hello1:echo1
01 echo "hello world"
01 hello world
✔ 01 xxx@127.0.0.1 0.272s
こんな感じ
その後の拡張のやり方
- これから開発していくときは、環境が増えたら、local.rbのように、xxx.rbを配置すればよい。
- hello1以外のタスクを定義をしたくなったら、hello1.rbのように、xxx.rbを配置すればよい。
おしまい。