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