Merge pull request #1514 from basecamp/hook-kamal-roles

Add KAMAL_ROLES to hook env variables
This commit is contained in:
Donal McBreen
2025-04-21 10:22:57 +01:00
committed by GitHub
4 changed files with 18 additions and 1 deletions

View File

@@ -133,7 +133,7 @@ module Kamal::Cli
def run_hook(hook, **extra_details) def run_hook(hook, **extra_details)
if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook) if !options[:skip_hooks] && KAMAL.hook.hook_exists?(hook)
details = { hosts: KAMAL.hosts.join(","), command: command, subcommand: subcommand } details = { hosts: KAMAL.hosts.join(","), roles: KAMAL.specific_roles&.join(","), command: command, subcommand: subcommand }.compact
say "Running the #{hook} hook...", :magenta say "Running the #{hook} hook...", :magenta
with_env KAMAL.hook.env(**details, **extra_details) do with_env KAMAL.hook.env(**details, **extra_details) do

View File

@@ -69,6 +69,7 @@ class Kamal::Cli::Proxy < Kamal::Cli::Base
end end
end end
when "get" when "get"
on(KAMAL.proxy_hosts) do |host| on(KAMAL.proxy_hosts) do |host|
puts "Host #{host}: #{capture_with_info(*KAMAL.proxy.boot_config)}" puts "Host #{host}: #{capture_with_info(*KAMAL.proxy.boot_config)}"
end end

View File

@@ -1,4 +1,5 @@
#!/bin/sh #!/bin/sh
echo "About to lock..." echo "About to lock..."
env
mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-connect mkdir -p /tmp/${TEST_ID} && touch /tmp/${TEST_ID}/pre-connect

View File

@@ -9,8 +9,12 @@ class MainTest < IntegrationTest
kamal :deploy kamal :deploy
assert_app_is_up version: first_version assert_app_is_up version: first_version
assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "pre-app-boot", "post-app-boot", "post-deploy" assert_hooks_ran "pre-connect", "pre-build", "pre-deploy", "pre-app-boot", "post-app-boot", "post-deploy"
assert_envs version: first_version assert_envs version: first_version
output = kamal :app, :exec, "--verbose", "ls", "-r", "web", capture: true
assert_hook_env_variables output, version: first_version
second_version = update_app_rev second_version = update_app_rev
kamal :redeploy kamal :redeploy
@@ -191,4 +195,15 @@ class MainTest < IntegrationTest
assert container_ids(vm: vm).any? assert container_ids(vm: vm).any?
end end
end end
def assert_hook_env_variables(output, version:)
assert_match "KAMAL_VERSION=#{version}", output
assert_match "KAMAL_SERVICE=app", output
assert_match "KAMAL_SERVICE_VERSION=app@#{version[0..6]}", output
assert_match "KAMAL_COMMAND=app", output
assert_match "KAMAL_PERFORMER=deployer@example.com", output
assert_match /KAMAL_RECORDED_AT=\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ/, output
assert_match "KAMAL_HOSTS=vm1,vm2", output
assert_match "KAMAL_ROLES=web", output
end
end end