Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kamal::Secrets did not load environment variables in .env on my Apple M1 #1038

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/kamal/secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ class Kamal::Secrets

Kamal::Secrets::Dotenv::InlineCommandSubstitution.install!

attr_reader :secrets
def initialize(destination: nil)
@secrets_files = \
[ ".kamal/secrets-common", ".kamal/secrets#{(".#{destination}" if destination)}" ].select { |f| File.exist?(f) }
@mutex = Mutex.new
::Dotenv.load
@secrets = set_secrets
end

def [](key)
Expand All @@ -29,7 +32,7 @@ def to_h
end

private
def secrets
def set_secrets
@secrets ||= secrets_files.inject({}) do |secrets, secrets_file|
secrets.merge!(::Dotenv.parse(secrets_file))
end
Expand Down
21 changes: 21 additions & 0 deletions test/secrets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,25 @@ class SecretsTest < ActiveSupport::TestCase
assert_equal "JKL", Kamal::Secrets.new(destination: "nodest")["SECRET2"]
end
end

test "dotenv load" do
create_and_remove_secret_files do
secrets = Kamal::Secrets.new
assert_equal @value, secrets[@key]
end
end

private

def create_and_remove_secret_files
@key = "ABC"
@value = "SECRET"
File.write(".env", "#{ @key }=#{ @value }")
Dir.mkdir(".kamal") if !Dir.exist?(".kamal")
File.write(".kamal/secrets", "#{ @key }=$#{ @key }")
yield
File.delete(".env")
File.delete(".kamal/secrets")
Dir.delete(".kamal")
end
end