The hook is run before the environment is loaded or the config is parsed. This makes it a bit of a special case - it doesn't have the usual KAMAL_XYZ environment variables, as we haven't parsed the config. The use case for this is to do auth checking or setup. So for example we can confirm you are logged in to a secret manager, and then you can directly call it to load your secrets in the .kamal/.env file using .dotenv's [command substitution](https://github.com/bkeepers/dotenv?tab=readme-ov-file#command-substitution).
50 lines
1.4 KiB
Ruby
50 lines
1.4 KiB
Ruby
require "test_helper"
|
|
|
|
class CommandsHookTest < ActiveSupport::TestCase
|
|
include ActiveSupport::Testing::TimeHelpers
|
|
|
|
setup do
|
|
freeze_time
|
|
|
|
@config = {
|
|
service: "app", image: "dhh/app", registry: { "username" => "dhh", "password" => "secret" }, servers: [ "1.1.1.1" ],
|
|
traefik: { "args" => { "accesslog.format" => "json", "metrics.prometheus.buckets" => "0.1,0.3,1.2,5.0" } }
|
|
}
|
|
|
|
@performer = Kamal::Git.email.presence || `whoami`.chomp
|
|
@recorded_at = Time.now.utc.iso8601
|
|
end
|
|
|
|
test "run" do
|
|
assert_equal [
|
|
".kamal/hooks/foo",
|
|
{ env: {
|
|
"KAMAL_RECORDED_AT" => @recorded_at,
|
|
"KAMAL_PERFORMER" => @performer,
|
|
"KAMAL_VERSION" => "123",
|
|
"KAMAL_SERVICE_VERSION" => "app@123",
|
|
"KAMAL_SERVICE" => "app" } }
|
|
], new_command.run("foo")
|
|
end
|
|
|
|
test "run with custom hooks_path" do
|
|
ENV["KAMAL_HOOKS_PATH"] = "custom/hooks/path"
|
|
assert_equal [
|
|
"custom/hooks/path/foo",
|
|
{ env: {
|
|
"KAMAL_RECORDED_AT" => @recorded_at,
|
|
"KAMAL_PERFORMER" => @performer,
|
|
"KAMAL_VERSION" => "123",
|
|
"KAMAL_SERVICE_VERSION" => "app@123",
|
|
"KAMAL_SERVICE" => "app" } }
|
|
], new_command.run("foo")
|
|
ensure
|
|
ENV.delete("KAMAL_HOOKS_PATH")
|
|
end
|
|
|
|
private
|
|
def new_command(**extra_config)
|
|
Kamal::Commands::Hook.new(Kamal::Configuration.new(@config.merge(**extra_config), version: "123"))
|
|
end
|
|
end
|