- Published Date:Feb. 14, 2016
Article Summary
This article is how to use Capistrano3.
環境
- rails4.2.5 & centos6.5
- ruby2.3.0
- rbenv
- unicorn
About capistrano
Capistrano3 is deployment tool. This is writtern by Ruby, it is good for rails app.
Other programing languages can be also available Capistrano3. if you use rails app, you can use many convient functions.
Purposes of use
deployment is hard job. On the other hand, these tasks are delicate.
if you faile deployment operation, many customers have such a bad influence.
if you use capistrano, it is easy to deploy web app.
In terms of secirity, deployment should be executed automatically.
cap install
At first, Add Capistrano to your project's Gemfile.
group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' gem 'capistrano', '~> 3.2.1' gem 'capistrano-ssh-doctor', '~> 1.0' gem 'capistrano-rails' gem 'capistrano-rbenv', github: "capistrano/rbenv" gem 'capistrano-bundler', "~> 1.1.0" gem 'capistrano3-unicorn' # unicornを使っている場合のみ end
install.
rbenv exec bundle install
cap install.
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
cap command creates all necessary configuration files.
if your app need other environments, you make file manually.
In my case, Local env is development. Staging env is operation verification. Production env is public site.
So, my cap files are default settings.
Next step is to set deployment setting.
Setting Capfile
My enviroment uses rbenv. add capistrano/rbenv to Capfile.
require 'capistrano/rbenv'
confirm version of ruby which running rails.
[root@vagrant-centos65 ~]# ruby -v ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
add version of ruby to Capfile.
require 'capistrano/rbenv' set :rbenv_ruby, '2.3.0'
Next, add rbenv path to Capfile.
[root@vagrant-centos65 ~]# which rbenv /usr/local/rbenv/bin/rbenv
add "/usr/local/rbenv" to Capfile.
require 'capistrano/rbenv' set :rbenv_ruby, '2.3.0' set :rbenv_custom_path, '/usr/local/rbenv'
Use Bundler.
require 'capistrano/bundler'
add other libraries. assets compile, unicorn restart, db migrate, site-map update.
require 'capistrano/rbenv' set :rbenv_ruby, '2.3.0' set :rbenv_custom_path, '/usr/local/rbenv' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' require 'capistrano/rails' require 'capistrano/bundler' require 'capistrano3/unicorn'
config/deploy.rb
This step is to set deploy.rb. This file describes common deployment operation.
add git infomation to Capfile.
set :application, 'sampleweb' set :repo_url, 'git@xxxxxxxx:test/sample_web.git' # Default value for :scm is :git set :scm, :git
Capistrano get source code using git clone command. On Linux, Set information of git , as you can use git clone command.
rbenv uses the system Ruby.
set :rbenv_type, :system # :system or :user set :rbenv_ruby, '2.3.0'
add symbolic link to Capfile.
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets bundle public}
add pid file to Capfile.
By default, unicorn_rails will create a PID file. this path is not default.
set :unicorn_pid, -> {"#{shared_path}/tmp/pids/unicorn.pid"}
add default enviroment to Capfile.
set :default_env, { path: "/usr/local/rbenv/shims:/usr/local/rbenv/bin:$PATH" }
When you execute deploy, capistrano execute assets precompile. Code is follow.
namespace :assets do Rake::Task['deploy:assets:precompile'].clear_actions desc "Precompile assets" task :precompile do puts "-----nothing-------" end end
When you execute deploy, capistrano restart unicorn. Code is follow.
desc 'Restart application' task :restart do on roles(:app), in: :sequence, wait: 5 do invoke 'unicorn:restart' # Your restart mechanism here, for example: # execute :touch, release_path.join('tmp/restart.txt') end end
Define Multiple Stages
Capistrano3 can define multiple stages.
At first, define staging.rb.
I use vagrant as staging environment. write code of ssh login.
role :app, %w{root@192.168.33.50} role :web, %w{root@192.168.33.50} role :db, %w{root@192.168.33.50} set :ssh_options, { verbose: :debug, auth_methods: %w(password), password: "vagrant" }
deploy test
execute deploy.
vagrant ssh cd {project_folder} bundle exec cap staging deploy:check
if error occurs, fix error.
you should pay attention to user permission.
and you confirm as you can use git clone command.
execute command by root permission.
D, [2016-02-01T08:48:42.485315 #11434] DEBUG -- tcpsocket[3fe196317614]: received packet nr 55 type 96 len 12 I, [2016-02-01T08:48:42.485343 #11434] INFO -- net.ssh.connection.session[3fe1963eb554]: channel_eof: 7 D, [2016-02-01T08:48:42.485384 #11434] DEBUG -- tcpsocket[3fe196317614]: received packet nr 56 type 97 len 12 I, [2016-02-01T08:48:42.485407 #11434] INFO -- net.ssh.connection.session[3fe1963eb554]: channel_close: 7 D, [2016-02-01T08:48:42.485469 #11434] DEBUG -- tcpsocket[3fe196317614]: queueing packet nr 46 type 97 len 28 INFO [ca5786bf] Finished in 0.034 seconds with exit status 0 (successful).
check is success. On production environment, execute deploy.
deploy
execute production deploy.
I, [2016-01-26T06:40:47.922269 #745] INFO -- net.ssh.connection.session[3fdd4a672b9c]: channel_data: 66 29b DEBUG [159535d3] Successful ping of Google D, [2016-01-26T06:40:47.923078 #745] DEBUG -- tcpsocket[3fdd4a0a92ec]: received packet nr 465 type 94 len 44 I, [2016-01-26T06:40:47.923165 #745] INFO -- net.ssh.connection.session[3fdd4a672b9c]: channel_data: 66 27b DEBUG [159535d3] Successful ping of Bing D, [2016-01-26T06:40:47.923676 #745] DEBUG -- tcpsocket[3fdd4a0a92ec]: received packet nr 466 type 98 len 44 ... // something INFO [db1051b1] Finished in 0.102 seconds with exit status 0 (successful).
Conclusion
Capistrano3 is convenient deployment tool.
In my case, I use Capistrano 3 with java and php.
Production environment should be automated. I recommend that you define capistrano, before you start project.
See you.