LoginSignup
3
3

More than 5 years have passed since last update.

capistrano 3 書き方

Last updated at Posted at 2014-06-24

Ruby on Rails4で動いているサイトの、複数台のwebサーバに最新ソースを適用して、更新があれば再起動させたくなりましたので、capistranoのコードを書きました。

/config/deploy.rb
namespace :apache do
  task :deploy do
    on roles(:web) do
      within "git管理下のディレクトリ" do
        puts '> git pull'
        out = capture(:git, "pull")
        puts out
        if out != 'Already up-to-date.' 
          puts '> sudo service apache2 restart'
          puts capture(:sudo, "service apache2 restart")
        end
      end
    end
  end
end

webサーバの定義は、環境ごと(staging.rb,production.rbなど)に別ファイルで定義しております。

/config/deploy/staging.rb
 :
role :web, %w{hoge.com fuga.com}
 :

これをJenkinsに登録して、Jenkinsから実行することでタスクの実行者の履歴を残しています。

そもそもcapistranoのタスクの記述言語がわからず悩みましたが、なんとか動きました。
cd できないできない :tired_face: と思っていたら、within で行けるのですねー。 :smiley:

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3