Capistranoにキャピキャピ(死語)してみた

Ruby

ぬにょす(挨拶)。

自動デプロイツールのCapistranoを使って、ステージング環境にデプロイしてみます。

スポンサーリンク

前提条件

  • デプロイ元
    • Railsアプリケーションを構築済み。
    • デプロイ先へのSSH疎通確認済み。
    • GitHubへのSSH疎通確認済み。
    • GitHubにRailsアプリケーションのリポジトリを作成済み。
  • デプロイ先
    • Railsアプリケーションの稼働環境(Ruby, MariaDB, Nginx等)を構築済み。
    • Railsアプリケーションが使用するデータベースを作成済み。
    • GitHubへのSSH疎通確認済み。

Capistranoのインストール

RailsプロジェクトのGemfileを編集します。

group :development do # 追記ここから gem "capistrano", "~> 3.11" gem "capistrano-rails", "~> 1.4" gem 'capistrano-rbenv', '~> 2.1' gem 'capistrano-bundler', '~> 1.5' gem "capistrano3-puma" gem 'ed25519' gem 'bcrypt_pbkdf' # 追記ここまで end
Code language: Ruby (ruby)

Bundlerでインストールします。

$ bundle install
Code language: plaintext (plaintext)

基本的な設定ファイルを生成します。

$ bundle exec cap install $ echo "require 'capistrano/rails'" >> Capfile $ echo "require 'capistrano/rbenv'" >> Capfile $ echo "require 'capistrano/bundler'" >> Capfile $ echo "require 'capistrano/puma'" >> Capfile $ echo "install_plugin Capistrano::Puma" >> Capfile
Code language: plaintext (plaintext)

deploy.rbの編集

config/deploy.rbを編集します。

set :application, "my_app_name" set :repo_url, "git@example.com:me/my_app_name.git" set :deploy_to, "/var/www/my_app_name" set :format, :airbrusshset :log_level, :debug set :format_options, truncate: false append :linked_files, 'config/database.yml', 'config/master.key' append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads' set :rbenv_type, :user set :rbenv_ruby, '2.6.2' namespace :deploy do desc "Make sure local git is in sync with remote." task :confirm do on roles(:app) do puts "This stage is '#{fetch(:stage)}'. Deploying branch is '#{fetch(:branch)}'." puts 'Are you sure? [y/n]' ask :answer, 'n' if fetch(:answer) != 'y' puts 'deploy stopped' exit end end end desc "Push shared files" task :init_files do on roles(:app) do fetch(:linked_files).each do |file| unless test "[ -f #{shared_path.join file} ]" upload! file, "#{shared_path.join file}" end end end end desc "Initial Deploy" task :initial do on roles(:app) do before 'deploy:check:linked_files', :init_files invoke 'deploy' end end before :starting, :confirm end
Code language: Ruby (ruby)

デプロイ先への接続情報の設定

config/deploy 配下の staging.rb を編集します。

server "example.com", user: "deploy", roles: %w{app db web} set :ssh_options, keys: %w(/home/deploy/.ssh/id_rsa)
Code language: Ruby (ruby)

その他のファイルの準備

config/environments/production.rb を staging.rb としてコピーします。

$ cp -a config/environments/production.rb config/environments/staging.rb
Code language: plaintext (plaintext)

config/database.yml にステージング環境のDB情報を追加します。

staging: <<: *default database: my_app_name
Code language: YAML (yaml)

デプロイの実行

ファイル一式をGitHubにプッシュしてから実行します。

$ bundle exec cap staging deploy:initial
Code language: plaintext (plaintext)

2回目以降は deploy だけで良い。

$ bundle exec cap staging deploy
Code language: plaintext (plaintext)

staging を production に変えれば本番環境へのデプロイになるはずです。

コメント

タイトルとURLをコピーしました