Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

例は.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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした