#author("2016-04-22T13:13: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はtarコマンドを使っているのがちょっと嫌。
-できれば自作gemと置き換えたい。構造は単純でcopy.rakeファイルを該当するLOAD_PATHに設置したgemを作るだけでよいっぽい。
-該当するTar/Zip gemを使いtarコマンドを使わないようにする。
-FileListのexcludeを利用する。


**deploy_via:copyしたい(capistrano-scm-copy編) [#u725bdde]
-[[wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy]]を使う方法。

***設定 [#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を追加。
#pre{{
group :deployment do
  gem 'capistrano', '~> 3.0', require: false
  gem 'rubyzip', require: false
end
}}
-lib/capistrano/tasks/copy.rakeを作成。上記リンクからちょっとカスタマイズ。
#pre{{
require 'zip'

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
}}
-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とか)。

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