Skip to content

Commit

Permalink
Replace .env* with .kamal/env*
Browse files Browse the repository at this point in the history
By default look for the env file in .kamal/env to avoid clashes with
other tools using .env.

For now we'll still load .env and issue a deprecation warning, but in
future we'll stop reading those.
  • Loading branch information
djmb committed Sep 4, 2024
1 parent 0b5506f commit 5c4c33e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 22 deletions.
17 changes: 15 additions & 2 deletions lib/kamal/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ def reload_env

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, use .kamal/env* instead"
Dotenv.load(".env.#{destination}", ".env")
end
end
else
Dotenv.load(".env")
if File.exist?(".kamal/env")
Dotenv.load(".kamal/env")
elsif File.exist?(".env")
warn "Loading .env from the project root is deprecated, use .kamal/env instead"
Dotenv.load(".env")
end
end
end

Expand Down
22 changes: 18 additions & 4 deletions lib/kamal/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,25 @@ def init
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

unless Pathname.new(File.expand_path(env_template_path)).exist?
if destination = options[:destination]
env_template_path = ".env.#{destination}.erb"
env_path = ".env.#{destination}"
else
env_template_path = ".env.erb"
env_path = ".env"
end

if Pathname.new(File.expand_path(env_template_path)).exist?
warn "Loading #{env_template_path} from the project root is deprecated, use .kamal/env[.<DESTINATION>].erb instead"
end
end

if Pathname.new(File.expand_path(env_template_path)).exist?
Expand Down
31 changes: 17 additions & 14 deletions test/cli/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ class CliMainTest < CliTestCase
end

test "envify" do
with_test_dotenv(".env.erb": "HELLO=<%= 'world' %>") do
with_test_env_files("env.erb": "HELLO=<%= 'world' %>") do
run_command("envify")
assert_equal("HELLO=world", File.read(".env"))
assert_equal("HELLO=world", File.read(".kamal/env"))
end
end

Expand All @@ -453,32 +453,32 @@ class CliMainTest < CliTestCase
<% end -%>
EOF

with_test_dotenv(".env.erb": file) do
with_test_env_files("env.erb": file) do
run_command("envify")
assert_equal("HELLO=world\nKEY=value\n", File.read(".env"))
assert_equal("HELLO=world\nKEY=value\n", File.read(".kamal/env"))
end
end

test "envify with destination" do
with_test_dotenv(".env.world.erb": "HELLO=<%= 'world' %>") do
with_test_env_files("env.world.erb": "HELLO=<%= 'world' %>") do
run_command("envify", "-d", "world", config_file: "deploy_for_dest")
assert_equal "HELLO=world", File.read(".env.world")
assert_equal "HELLO=world", File.read(".kamal/env.world")
end
end

test "envify with skip_push" do
Pathname.any_instance.expects(:exist?).returns(true).times(1)
File.expects(:read).with(".env.erb").returns("HELLO=<%= 'world' %>")
File.expects(:write).with(".env", "HELLO=world", perm: 0600)
Pathname.any_instance.expects(:exist?).returns(true).times(2)
File.expects(:read).with(".kamal/env.erb").returns("HELLO=<%= 'world' %>")
File.expects(:write).with(".kamal/env", "HELLO=world", perm: 0600)

Kamal::Cli::Main.any_instance.expects(:invoke).with("kamal:cli:env:push").never
run_command("envify", "--skip-push")
end

test "envify with clean env" do
with_test_dotenv(".env": "HELLO=already", ".env.erb": "HELLO=<%= ENV.fetch 'HELLO', 'never' %>") do
with_test_env_files("env": "HELLO=already", "env.erb": "HELLO=<%= ENV.fetch 'HELLO', 'never' %>") do
run_command("envify", "--skip-push")
assert_equal "HELLO=never", File.read(".env")
assert_equal "HELLO=never", File.read(".kamal/env")
end
end

Expand Down Expand Up @@ -572,15 +572,18 @@ def run_command(*command, config_file: "deploy_simple")
end
end

def with_test_dotenv(**files)
def with_test_env_files(**files)
Dir.mktmpdir do |dir|
fixtures_dup = File.join(dir, "test")
FileUtils.mkdir_p(fixtures_dup)
FileUtils.cp_r("test/fixtures/", fixtures_dup)

Dir.chdir(dir) do
files.each do |filename, contents|
File.binwrite(filename.to_s, contents)
FileUtils.mkdir_p(".kamal")
Dir.chdir(".kamal") do
files.each do |filename, contents|
File.binwrite(filename.to_s, contents)
end
end
yield
end
Expand Down
4 changes: 2 additions & 2 deletions test/integration/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class MainTest < IntegrationTest

private
def assert_local_env_file(contents)
assert_equal contents, deployer_exec("cat .env", capture: true)
assert_equal contents, deployer_exec("cat .kamal/env", capture: true)
end

def assert_envs(version:)
Expand Down Expand Up @@ -143,7 +143,7 @@ def assert_env_files
end

def remove_local_env_file
deployer_exec("rm .env")
deployer_exec("rm .kamal/env")
end

def assert_remote_env_file(contents, vm:)
Expand Down

0 comments on commit 5c4c33e

Please sign in to comment.