kikeda1104's blog

備忘録・技術に関することを書いています。(webエンジニア)

capistranoの実装

Virtual boxに構築したCent OSにcapistrano 3を利用してデプロイできるようにしようかなと。awsが理想的だったんですが、お金がかかるのでやめました・・。

アプリはbe rails newで,それをローカルからCent OSに向けてデプロイします。

railsアプリの作成

be rails new hoge && cd hoge

Gemfileへのgemの追加

vi Gemfile
Gemfile
gem 'capistrano', "~> 3.1"
gem 'capistrano-rails'
bundle install --path=vendor/bundle
  出力を省略
  Your bundle is complete!
  It was installed into ./vendor/bundle

deploy.rbの作成

cap install
cd config/
vi deploy.rb

deploy.rbを編集します。

deploy.rb

set :application, 'manage_image'
set :repo_url, 'https://github.com/kikeda1104/manage_image.git' set :branch, 'master'
:app %{"#{アプリケーションサーバIP}"}
scm :git # デフォルトがgitになるので明示したければコメントアウトをはずす

前回の記事で書いたリモートサーバの公開鍵設定により config/deploy/production.rbから記述を移す必要がないと思うが事前に設定して動作することを確認したので (deploy先のステージが違うにも関わらず参照してしまう・・)

config/deploy/production.rb

server '192.68.56.2', user: 'root', roles: %w{ app } # 削除

config/deploy.rb

server '192.68.56.2', user: 'root', roles: %w{ app }, ssh_options: { keys: [File.expand_path('~/.ssh/id_rsa_vGuest')] }

これでRailsのtopディレクトリより be cap production deploy

INFO [ed187ab9] Running /usr/bin/env mkdir -p /tmp/manage_image/ on 192.168.56.2
DEBUG [ed187ab9] Command: /usr/bin/env mkdir -p /tmp/manage_image/
INFO [ed187ab9] Finished in 0.589 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/manage_image/git-ssh.sh 0.0%
INFO Uploading /tmp/manage_image/git-ssh.sh 100.0%
INFO [a9d57a94] Running /usr/bin/env chmod +x /tmp/manage_image/git-ssh.sh on 192.168.56.2
DEBUG [a9d57a94] Command: /usr/bin/env chmod +x /tmp/manage_image/git-ssh.sh                                          
INFO [a9d57a94] Finished in 0.008 seconds with exit status 0 (successful).
DEBUG [1183f885] Running /usr/bin/env git ls-remote https://github.com/kikeda1104/manage_image.git on 192.168.56.2
DEBUG [1183f885] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/manage_image/git-ssh.sh /usr/bin/env git ls-remote https://github.com/kikeda1104/manage_image.git )
DEBUG [1183f885]  18e9fb308516d55d83aa4a8f98a3bf129dd9deef  HEAD
DEBUG [1183f885]  18e9fb308516d55d83aa4a8f98a3bf129dd9deef  refs/heads/master
DEBUG [1183f885] Finished in 1.618 seconds with exit status 0 (successful).
INFO [0b574cf3] Running /usr/bin/env mkdir -pv /var/www/shared /var/www/releases on 192.168.56.2
DEBUG [0b574cf3] Command: /usr/bin/env mkdir -pv /var/www/shared /var/www/releases
INFO [0b574cf3] Finished in 0.082 seconds with exit status 0 (successful).
DEBUG [f3aa22d3] Running /usr/bin/env [ -f /var/www/repo/HEAD ] on 192.168.56.2
DEBUG [f3aa22d3] Command: [ -f /var/www/repo/HEAD ]
DEBUG [f3aa22d3] Finished in 0.008 seconds with exit status 0 (successful).
INFO The repository mirror is at /var/www/repo
DEBUG [5700ddfc] Running /usr/bin/env if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi on 192.168.56.2
DEBUG [5700ddfc] Command: if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi
DEBUG [5700ddfc] Finished in 0.008 seconds with exit status 0 (successful).
DEBUG [48a887ff] Running /usr/bin/env cd /var/www/repo && git rev-parse --short HEAD on 192.168.56.2
DEBUG [48a887ff] Command: cd /var/www/repo && git rev-parse --short HEAD
DEBUG [48a887ff]   18e9fb3
DEBUG [48a887ff] Finished in 0.060 seconds with exit status 0 (successful).
INFO [ed41246d] Running /usr/bin/env git remote update on 192.168.56.2
DEBUG [ed41246d] Command: cd /var/www/repo && /usr/bin/env git remote update
DEBUG [ed41246d]  Fetching origin
INFO [ed41246d] Finished in 1.080 seconds with exit status 0 (successful).
DEBUG [4fe9bfbb] Running /usr/bin/env cd /var/www/repo && git rev-parse --short HEAD on 192.168.56.2
DEBUG [4fe9bfbb] Command: cd /var/www/repo && git rev-parse --short HEAD
DEBUG [4fe9bfbb]   18e9fb3
DEBUG [4fe9bfbb] Finished in 0.010 seconds with exit status 0 (successful).
DEBUG [d5d669e7] Running /usr/bin/env if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi on 192.168.56.2
DEBUG [d5d669e7] Command: if test ! -d /var/www/repo; then echo "Directory does not exist '/var/www/repo'" 1>&2; false; fi
DEBUG [d5d669e7] Finished in 0.008 seconds with exit status 0 (successful).
INFO [4866fb67] Running /usr/bin/env mkdir -p /var/www/releases/20140129080400 on 192.168.56.2
DEBUG [4866fb67] Command: cd /var/www/repo && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/manage_image/git-ssh.sh /usr/bin/env mkdir -p /var/www/releases/20140129080400 )
INFO [4866fb67] Finished in 0.009 seconds with exit status 0 (successful).
INFO [abd0b656] Running /usr/bin/env git archive master | tar -x -C /var/www/releases/20140129080400 on 192.168.56.2
DEBUG [abd0b656] Command: cd /var/www/repo && ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/manage_image/git-ssh.sh /usr/bin/env git archive master | tar -x -C /var/www/releases/20140129080400 )
INFO [abd0b656] Finished in 0.177 seconds with exit status 0 (successful).
INFO [e55a9003] Running /usr/bin/env rm -rf /var/www/current on 192.168.56.2
DEBUG [e55a9003] Command: /usr/bin/env rm -rf /var/www/current
INFO [e55a9003] Finished in 0.010 seconds with exit status 0 (successful).
INFO [11b12ca9] Running /usr/bin/env ln -s /var/www/releases/20140129080400 /var/www/current on 192.168.56.2
DEBUG [11b12ca9] Command: /usr/bin/env ln -s /var/www/releases/20140129080400 /var/www/current
INFO [11b12ca9] Finished in 0.009 seconds with exit status 0 (successful).
INFO [cc92a406] Running /usr/bin/env mkdir /var/www/releases/20140129080400/tmp on 192.168.56.2
DEBUG [cc92a406] Command: /usr/bin/env mkdir /var/www/releases/20140129080400/tmp
INFO [cc92a406] Finished in 0.012 seconds with exit status 0 (successful).
INFO [997bd327] Running /usr/bin/env touch /var/www/releases/20140129080400/tmp/restart.txt on 192.168.56.2
DEBUG [997bd327] Command: /usr/bin/env touch /var/www/releases/20140129080400/tmp/restart.txt
INFO [997bd327] Finished in 0.010 seconds with exit status 0 (successful).
DEBUG [6b97aeb0] Running /usr/bin/env ls -x /var/www/releases on 192.168.56.2
DEBUG [6b97aeb0] Command: /usr/bin/env ls -x /var/www/releases
DEBUG [6b97aeb0]   20140128111131  20140128112742  20140129080400
DEBUG [6b97aeb0] Finished in 0.013 seconds with exit status 0 (successful).
DEBUG [06112fe5] Running /usr/bin/env if test ! -d /var/www/releases; then echo "Directory does not exist '/var/www/releases'" 1>&2; false; fi on 192.168.56.2
DEBUG [06112fe5] Command: if test ! -d /var/www/releases; then echo "Directory does not exist '/var/www/releases'" 1>&2; false; fi
DEBUG [06112fe5] Finished in 0.009 seconds with exit status 0 (successful).
INFO [87c42f7c] Running /usr/bin/env echo "Branch master (at 18e9fb3) deployed as release 20140129080400 by Kikeda; " >> /var/www/revisions.log on 192.168.56.2
DEBUG [87c42f7c] Command: echo "Branch master (at 18e9fb3) deployed as release 20140129080400 by Kikeda; " >> /var/www/revisions.log
INFO [87c42f7c] Finished in 0.062 seconds with exit status 0 (successful). 

これでデプロイの完了。

URL capistranoの公式サイト

capistrano github

http://stackoverflow.com/questions/20288969/capistrano-netsshauthenticationfailed