#author("2016-04-22T13:13:57+09:00","default:wikiwriter","wikiwriter")
#author("2017-04-12T15:20:57+09:00","default:wikiwriter","wikiwriter")
[[Capistrano3]]

&tag(Capistrano3/deploy_via_copyする);
*目次 [#i7bdbafb]
#contents
*関連ページ [#d98e9a99]
-[[../基本]]
-[[./古い情報]]

*参考情報 [#xcebfe5f]

**deploy_via:copyしたい(自作gem編) [#a88c0fbe]
TODO:
*capistrano-scm-copyを最初から使う方法 [#ha3bcf1a]

-capistrano-scm-copyはtarコマンドを使っているのがちょっと嫌。
-できれば自作gemと置き換えたい。構造は単純でcopy.rakeファイルを該当するLOAD_PATHに設置したgemを作るだけでよいっぽい。
-該当するTar/Zip gemを使いtarコマンドを使わないようにする。
-FileListのexcludeを利用する。
**インストール [#pbd49b81]
-Gemfileを編集。rubyzipが必要なことに注意。
#pre{{
gem 'rubyzip'

group :deployment do
  gem 'capistrano'
  gem 'capistrano-rails'
  gem 'capistrano-rbenv'
  gem 'capistrano-bundler'
  gem "capistrano-scm-copy"
end
}}
-gemのインストール
 bundle install --path vendor/bundle
-cap install実行
 bundle exec cap install

**deploy_via:copyしたい(capistrano-scm-copy編) [#u725bdde]
**設定ファイルの編集 [#pfa01924]
-Capfileの編集。rbenv、bundler、assets、migrationsあたりが必要。
#pre{{
# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
}}
-config/deploy/staging.rb、production.rbの編集
#pre{{
puts "\n\e[0;31m Are you REALLY sure you want to deploy to production? \e[0m\n"
proceed = STDIN.gets
unless proceed =~ /^y/
  puts "canceld"
  exit
end

set :ssh_options, :forward_agent=>true, :keys=>'~/.ssh/id_rsa.myapp_np', :auth_methods=>%w(publickey)

set :deploy_to, '/Users/sora/www/myapp'
server 'bronze', user: 'sora', roles: %w{web}, my_property: :my_value

role :app, %w{bronze}
role :web, %w{bronze}
role :db,  %w{bronze}

}}
-config/deploy.rbの編集
#pre{{

set :application, 'myapp'
set :rbenv_ruby, File.read('.ruby-version').strip

set :scm, :copy
set :include_dir, ["*", '.ruby-version']
set :exclude_dir, ['vendor/bundle', 'log\/*', 'tmp\/*', 'mydb\/*']
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle')

set :keep_releases, 3

task :list do
  on roles(:web) do |host|
    execute "ls"
  end
end

namespace :deploy do
  namespace :deploy do
    after :restart, :restart_passenger do
      on roles(:web), in: :groups, limit: 3, wait: 10 do
        within release_path do
          execute :touch, 'tmp/restart.txt'
        end
      end
    end
    after :finishing, 'deploy:restart_passenger', 'deploy:cleanup'
  end
end

}}
**使用する [#oc86e354]
-サーバー上で"ls"を実行する"list"タスクを実行
 bundle exec cap production list
-deployする
 bundle exec cap staging deploy


*capistrano-scm-copyを後から追加する方法[#u725bdde]
-[[wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy]]を使う方法。

***設定 [#fddf8ec0]
**設定 [#fddf8ec0]
-Gemfileに以下を追加する
 gem "capistrano-scm-copy"
-config/deploy.rbの:scmを変更。exclude_dirに指定したディレクトリは無視される。
 set :scm, :copy
 set :exclude_dir, ['vendor/bundle']
-staging.rb、production.rbの変更
-deploy_toでdeploy先ディレクトリを指定する。staging、production環境で異なる場合deploy.rbではなくstaging.rbかproduction.rbで変更しないといけない。
#pre{{
 set :deploy_to, '/home/tanaka/www/demoapp'
}}

-公式ドキュメントによろと、Capfileに以下を追加する必要がある。
 require 'capistrano/copy'
-しかし、[[Appears to try to upload archive file 2x · Issue #17 · wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy/issues/17]]によると、処理が2回走ってしまうためrequireしてはならない。
#pre{{
I had the same problem but could find a proper solution; remove require 'capistrano/copy' anywhere!

This problem occurs when we use this module with capistrano/setup because capistrano/setup loads a scm module with the :scm value.
https://github.com/capistrano/capistrano/blob/master/lib/capistrano/setup.rb#L18

Good luck!
}}
**deploy_via:copyしたい(自作編) [#n43b9a84]
-デフォルトではできなくなっているので、[[Capistrano3 で deploy_via :copy する - Qiita:http://qiita.com/hidakatsuya/items/4d097416516afc229199]]を参考にする。
-Gemfileにrubyzipを追加。

*トラブルシューティング [#b298585a]
-[[IMplement the new Capistrano Scm plugin system · Issue #34 · wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy/issues/34]]にあるよに、Capistrano 3.7以降で警告が表示されるようになった。
#pre{{
group :deployment do
  gem 'capistrano', '~> 3.0', require: false
  gem 'rubyzip', require: false
end
}}
-lib/capistrano/tasks/copy.rakeを作成。上記リンクからちょっとカスタマイズ。
#pre{{
require 'zip'
[Deprecation Notice] `set :scm, :copy` is deprecated.
To ensure this custom SCM will work with future versions of Capistrano,
please upgrade it to a version that uses the new SCM plugin mechanism
documented here:

Zip.setup do |c|
  c.unicode_names = true
  c.on_exists_proc = true
  c.continue_on_exists_proc = true
end

namespace :copy do
  task :check do
  end

  task :set_current_revision do
  end

  task create_release: 'release.zip' do |t|
    file = t.prerequisites.first
    on roles(:app) do
      execute :mkdir, '-p', fetch(:tmp_dir)
      upload! file, fetch(:tmp_dir)
      execute :unzip, '-o', "#{fetch(:tmp_dir)}/release.zip", '-d', release_path
    end
    File.delete file if File.exists?(file)
  end

  file 'release.zip' do |t|
    release_filename = File.join(Dir.pwd, t.name)

    Dir.chdir fetch(:copy_dir) do
      Zip::File.open(release_filename, Zip::File::CREATE) do |zipfile|
        files = FileList['**/*']
        files.exclude(*fetch(:copy_exclude)).each do |file|
          zipfile.add(file, file)
        end
      end
    end
  end
end
http://capistranorb.com/documentation/advanced-features/custom-scm
}}
-deploy.rbを編集して完了。
#pre{{
set :scm, 'copy'
set :copy_dir, '.'
set :tmp_dir, '/tmp'
set :copy_exclude, [ /\.log$/, %r!^files/.+! ]
}}
-copy_excludeのパターンは、 Rake::FileListの、excludeで指定できるパターン。[[class Rake::FileList:http://docs.ruby-lang.org/ja/2.0.0/class/Rake=3a=3aFileList.html#I_EXCLUDE]]。パターンとして正規表現、グロブパターン、文字列が使用可能。
-FileList['**/*']はプロジェクトルートからの相対パスを返してくるので、例えば railsプロジェクトの、logs、vendor以下を除外したい場合、以下のように指定するとよさそう。
#pre{{
  set :copy_exclude, [ 
                      /^logs/,
                      /^tmp/,
                      /^vendor/
                     ]
}}
-ドットファイル(.で始まるファイル)がコピーされないけど、それはそれでいいのか?(特にruby-versionとか)。
-[[Capistrano 3 でデプロイサーバから対象サーバにコピー - Qiita:http://qiita.com/Ping/items/57133b6bbb0a3d705fed]]にあるように、[[ydkn/capistrano-git-copy: Capistrano plugin that packages git repository locally and uploads it to server:https://github.com/ydkn/capistrano-git-copy]]を使うのがいいかもしれない。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS