Read from .kamal/.env

To avoid conflicts with other tools that use .env files, read the files
from .kamal/ instead.

If there are no matching env files in .kamal/, we'll read from the
project root for now and emit a warning.
This commit is contained in:
Donal McBreen
2024-07-30 12:29:44 +01:00
parent ec4aa45852
commit a8837d453c
6 changed files with 34 additions and 17 deletions

View File

@@ -37,9 +37,23 @@ module Kamal::Cli
def load_env
if destination = options[:destination]
Dotenv.load(".env.#{destination}", ".env")
if File.exist?(".kamal/.env.#{destination}") || File.exist?(".kamal/.env")
Dotenv.load(".kamal/.env.#{destination}", ".kamal/.env")
else
loading_files = [ (".env" if File.exist?(".env")), (".env.#{destination}" if File.exist?(".env.#{destination}")) ].compact
if loading_files.any?
warn "Loading #{loading_files.join(" and ")} from the project root, in future they will be loaded from .kamal/"
Dotenv.load(".env.#{destination}", ".env")
end
end
else
Dotenv.load(".env")
if File.exist?(".kamal/.env")
Dotenv.load(".kamal/.env")
elsif File.exist?(".env")
$stderr.puts caller
warn "Loading .env from the project root, in future it will be loaded then from .kamal/.env"
Dotenv.load(".env")
end
end
end

View File

@@ -179,15 +179,15 @@ class Kamal::Cli::Main < Kamal::Cli::Base
end
end
desc "envify", "Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)"
desc "envify", "Create .kamal/.env by evaluating .kamal/.env.erb (or .kamal/.env.staging.erb -> .kamal/.env.staging when using -d staging)"
option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip .env file push"
def envify
if destination = options[:destination]
env_template_path = ".env.#{destination}.erb"
env_path = ".env.#{destination}"
env_template_path = ".kamal/.env.#{destination}.erb"
env_path = ".kamal/.env.#{destination}"
else
env_template_path = ".env.erb"
env_path = ".env"
env_template_path = ".kamal/.env.erb"
env_path = ".kamal/.env"
end
if Pathname.new(File.expand_path(env_template_path)).exist?