Add KAMAL_HOST to app and accessory containers

Adds the host the container is being deployed to as KAMAL_HOST.
My use case is to more easily tag the host for metrics tagging,
but there might be other uses as well.
This commit is contained in:
Jake Prem
2025-03-25 22:49:00 -04:00
parent 8fe2f92164
commit 0e453a02de
3 changed files with 5 additions and 3 deletions

View File

@@ -13,11 +13,11 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
directories(name)
upload(name)
on(hosts) do
on(hosts) do |host|
execute *KAMAL.auditor.record("Booted #{name} accessory"), verbosity: :debug
execute *accessory.ensure_env_directory
upload! accessory.secrets_io, accessory.secrets_path, mode: "0600"
execute *accessory.run
execute *accessory.run(host: host)
if accessory.running_proxy?
target = capture_with_info(*accessory.container_id_for(container_name: accessory.service_name, only_running: true)).strip

View File

@@ -13,11 +13,12 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base
@accessory_config = config.accessory(name)
end
def run
def run(host: nil)
docker :run,
"--name", service_name,
"--detach",
"--restart", "unless-stopped",
*([ "-e", "KAMAL_HOST=\"#{host}\"" ] if host),
*network_args,
*config.logging_args,
*publish_args,

View File

@@ -22,6 +22,7 @@ class Kamal::Commands::App < Kamal::Commands::Base
*([ "--hostname", hostname ] if hostname),
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
"-e", "KAMAL_VERSION=\"#{config.version}\"",
"-e", "KAMAL_HOST=\"#{host}\"",
*role.env_args(host),
*role.logging_args,
*config.volume_args,