ERROR: While executing gem ... (Gem::Package::PathError) installing into parent path /Users/shoyan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/capistrano/v3/tasks/whenever.rake of /Users/shoyan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/whenever-0.9.5 is not allowed
on ['deploy@example.com’] do |host| puts capture(:hostname) end => example.com
on メソッドに対象のサーバーとブロックを渡します。 対象のサーバーは複数設定することも可能です。 ブロックにはサーバー上で実行するコマンドを設定します。 captureメソッドは渡された引数をコマンドとして実行し、結果をログに出力します。
特定のユーザーでコマンドを実行する
特定のユーザーでコマンドを実行する場合は、asメソッドで指定します。
1 2 3 4 5
on ['example.com'] do|host| as 'deploy'do puts capture(:whoami) end end
特定のディレクトリを指定する
特定のディレクトリを指定する場合は、withinメソッドを指定します。
1 2 3 4 5
on ['deploy@example.com'] do|host| within '/var/log'do puts capture(:head, '-n5', 'messages') end end
/var/log で head -n5 messages を実行します。
環境変数を指定する
withメソッドで環境変数を指定することができます。
1 2 3 4 5
on hosts do |host| with rack_env: :test do puts capture("env") end end
rack_envに:test を設定しています。
ファイルをチェックして存在すればメッセージを表示、なければファイルを作成する
1 2 3 4 5 6 7 8 9 10
on ['deploy@example.com'] do |host| f = '/tmp/file' if test("[ -f #{f} ]") info "#{f} already exist on #{host}!" else execute :touch, f end end INFO [790b6aaa] Running /usr/bin/env touch /tmp/file as deploy@example.com INFO [790b6aaa] Finished in0.052 seconds withexit status 0 (successful).
desc "Deploy the site, pulls from Git, migrate the db and precompile assets, then restart Passenger." task :deploy do include SSHKit::DSL
on "example.com" do |host| within "/opt/sites/example.com" do execute :git, :pull execute :bundle, :install, '--deployment' execute :rake, 'db:migrate' execute :rake, 'assets:precompile' execute :touch, 'tmp/restart.txt' end end end