Skip to content

Instantly share code, notes, and snippets.

@wang-zhijun
Last active February 18, 2020 11:14
Show Gist options
  • Save wang-zhijun/682f82dd7c4f6d8bb185 to your computer and use it in GitHub Desktop.
Save wang-zhijun/682f82dd7c4f6d8bb185 to your computer and use it in GitHub Desktop.
Capistranoメモ

Capistranoを使うメモ

$ bundle init

GemfileにCapistranoを入れる

 # A sample Gemfile
 source "https://rubygems.org"

 # gem "rails"
 group :development do
   gem "capistrano", "~> 3.5"
 end

インストールします。下の方法はもちろん、$ gem install capistranoコマンドでもインストールしてくれます。

$ bundle install
$ bundle exec cap install # Capistrano使うために必要なファイルを生成
$ bundle exec cap -T # 有効なタスクを定義する

Your new Capfile will automatically include any tasks from any *.rake files in lib/capistrano/tasks.

Capistrano breaks down common tasks into a notion of roles, that is, taking a typical Rails application that we have roughly speaking three roles, web, app, and db.

タスクlsを定義してみる。まずconfig/deploy/staging.rbファイルを修正する。

# server-based syntax
server '*.*.*.*', user: 'wang', roles: %w{app}

# role-based syntax
role :app, %w{wang@*.*.*.*}

# ssh関する設定
server '*.*.*.*',
  user: 'wang',
  roles: %w{app},
  ssh_options: {
    user: 'wang',
    keys: %w(/Users/<your-name>/.ssh/id_rsa),
    forward_agent: false,
    auth_methods: %w(publickey password),
    # password: 'please use keys'
    port: 22
   }

接続情報を修正したあとconfig/deploy.rbファイルにタスクlsを定義する。

namespace :deploy do
  path = "some variable"
  desc "list remote server file"
  task :list do
    on roles(:all) do
      execute "ls"
    end
  end
end

lsタスクはネームスペースdeployに含まれているため、実行する時にdeploy:listのように指定が必要

bundle exec cap staging deploy:list

capistrano-stats とは関係ないですが、 Capfile locked at 3.4.0, but 3.5.0 is loaded と出るときは config/deploy.rb の最初の方にある lock '3.4.0' という行を lock '3.5.0' に書き換えれば OK です。


Gemfileのgroupについて

groupコマンドではgemをインストールする環境を指定できる。

group :development, :test do
  gem "rspec-rails"
  gem "cucumber"
end

のように設定した上で本番環境で

$ bundle install --without development test

特に何も指定しない場合は「staging」と「production」用の設定ファイルが作成される。 あとで追加もできるが、初めに作って欲しい設定ファイルがある場合は以下の様に設定することも可能。

$ bundle exec cap install STAGES=development,staging,production

リモートの各ブランチの先頭commit idを取得

git ls-remote --heads https://github.com/coldfreak/cookbook-setupvm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment