LoginSignup
3
2

More than 5 years have passed since last update.

Capistranoのuploadでリモートとローカルのファイルを比較して上書き確認する

Last updated at Posted at 2015-02-23

用途としては、サーバー上の設定ファイルをマニュアルで何度も変更して検証する場合や、複数の人がサーバー上の設定ファイルを変更する可能性がある場合など、その後再デプロイするときに設定ファイルの戻し忘れによる事故を防ぐために使う

例は.env.productionのアップロード (dotenv-deployment gem)

filepath = ...
remote_filepath = "#{shared_path}/.env.production"

if test("[ -f #{remote_filepath} ]")
  local_content = File.read(filepath)
  remote_content = capture("cat #{remote_filepath}")
  if local_content.strip == remote_content.strip
    puts "Skip uploading of #{filepath} because files are identical"
  else
    puts "Diff of #{filepath}"
    puts "--- Local file ---"
    puts local_content.strip.split("\n") - remote_content.strip.split("\n")
    puts "--- Remote file ---"
    puts remote_content.strip.split("\n") - local_content.strip.split("\n")
    puts
    ask(:continue_upload, "Do you continue `deploy:upload_dotenv`? [y/n]")
    if fetch(:continue_upload) == 'y'
      upload!(filepath, remote_filepath)
    end
  end
else
  upload!(filepath, remote_filepath)
end

ログの出力例

Diff of config/deploy/.env.production
--- Local file ---
FOO=12345678
BAR=abcdefg
--- Remote file ---
FOO=00000000

Please enter continue_upload (Do you continue `deploy:upload_dotenv`? [y/n]): y
DEBUGUploading config/deploy/.env.production 0.0%
INFOUploading config/deploy/.env.production 100.0%

あまりきれいだと思わないが、とりあえず動く


Capistranoには独自のaskメソッドがある
https://github.com/capistrano/capistrano/blob/master/lib/capistrano/configuration.rb

参考:Writing Capistrano Tasks

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2