Skip to content

Set KAMAL_DESTINATION before rendering the config for an alias - #1968

Open
viktorianer wants to merge 1 commit into
basecamp:mainfrom
viktorianer:set-destination-before-resolving-aliases
Open

viktorianer wants to merge 1 commit into
basecamp:mainfrom
viktorianer:set-destination-before-resolving-aliases

Conversation

@viktorianer

Copy link
Copy Markdown

kamal <alias> -d staging resolves the alias through Commander#resolve_alias, which calls Configuration.load_raw_config before any Configuration.create_from. Only create_from sets ENV["KAMAL_DESTINATION"], so the ERB in deploy.yml renders without the destination the command was given.

A config that reads the destination at render time then resolves the alias against the wrong values. A common shape is loading a per-destination env file:

<% require "dotenv"; Dotenv.load(".env.#{ENV.fetch("KAMAL_DESTINATION", "production")}") %>

With an alias, this loads .env.production for -d staging. It raises when that file is absent, or silently renders production values before the real command runs with the staging config. The same command without an alias renders correctly.

ENV["KAMAL_DESTINATION"] is now set in load_raw_config, which both create_from and resolve_alias call.

The new test in test/cli/main_test.rb runs an alias whose command renders KAMAL_DESTINATION. It fails without the change, because the command is rendered as bin/console, not bin/console world.

An alias is resolved by loading the raw config before Configuration.create_from
runs, so ERB in deploy.yml rendered without KAMAL_DESTINATION even when
--destination was given. A config that picks its env file or values from the
destination then resolved the alias against the wrong ones.

Setting the variable in load_raw_config covers both paths.
Copilot AI balanced review requested due to automatic review settings October 12, 2026 20:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

The change addresses the alias rendering order while preserving existing configuration behavior and adding focused regression coverage.

0 open findings

What changed in this PR

Sets KAMAL_DESTINATION before rendering configuration, ensuring aliases use destination-specific ERB values.

Changes:

  • Moves destination environment setup into load_raw_config.
  • Adds an alias integration test and destination fixtures.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
lib/​kamal/​configuration.rb Sets the destination before ERB rendering.
test/​cli/​main_test.rb Tests destination-aware alias rendering.
test/​fixtures/​deploy_with_destination_aliases.yml Defines an ERB-based alias fixture.
test/​fixtures/​deploy_with_destination_aliases.world.yml Supplies the destination-specific server fixture.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants