Skip to content

Export pre-configure hook output to ENV for config ERB - #1926

Open
rience wants to merge 1 commit into
basecamp:post-configurefrom
rience:pre-configure-env
Open

Export pre-configure hook output to ENV for config ERB#1926
rience wants to merge 1 commit into
basecamp:post-configurefrom
rience:pre-configure-env

Conversation

@rience

@rience rience commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Builds on #1783. The pre-configure hook can already rewrite the destination and pass data to subsequent hooks via $KAMAL_OUTPUT, but its output is invisible to the ERB in the config file itself — the one consumer that runs immediately after it.

This exports every non-reserved key from the hook's output into ENV before Configuration.create_from runs, so a hook can compute dynamic values that deploy.yml reads with plain <%= ENV["..."] %>:

# .kamal/hooks/pre-configure
echo "MY_HOST=$(./bin/resolve_branch_host)" >> "$KAMAL_OUTPUT"
servers:
  - <%= ENV["MY_HOST"] %>

KAMAL_DESTINATION and KAMAL_MESSAGE keep their existing special semantics and are not exported.

Motivation

We deploy a monorepo (~20 apps) where every generated deploy.*.yml currently starts with an ERB line that loads an internal library and mutates ENV in-process — it even re-parses -d out of ARGV — so the rest of the YAML can read <%= ENV[...] %>. It works only because Kamal renders ERB in-process, and it's exactly the kind of thing pre-configure should replace. Since config-file ERB has access to nothing but ENV, this back-channel is the missing link for feeding hook-computed values into the config.

Test plan

  • Hook output key becomes an env var visible to config ERB (servers: - <%= ENV["PRE_CONFIGURE_HOST"] %> resolves)
  • Reserved keys (KAMAL_DESTINATION, KAMAL_MESSAGE) are not exported
  • Existing pre-configure suite still green (14 runs, 0 failures)

🤖 Generated with Claude Code

The pre-configure hook can already rewrite the destination and pass
data to subsequent hooks, but its output was invisible to the ERB in
the config file itself. Export non-reserved keys into ENV before the
config is created, so a hook can compute dynamic values (hosts,
per-branch settings, etc.) that deploy.yml reads via ENV.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings August 7, 2026 07:18

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.

Pull request overview

Exports non-reserved pre-configure hook output to ENV for configuration ERB rendering.

Changes:

  • Exports hook output except reserved keys.
  • Documents ERB access in the sample hook.
  • Adds integration coverage and configurable test YAML.

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.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/kamal/cli/base.rb Exports non-reserved hook output to ENV.
lib/kamal/cli/templates/sample_hooks/pre-configure.sample Documents configuration ERB usage.
test/cli/pre_configure_test.rb Tests export behavior and extends test setup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +60 to +66
test "pre-configure hook does not export reserved keys to ENV" do
with_pre_configure_hook({ "KAMAL_MESSAGE" => "Deploying to beta2" }) do
run_command("exec", "date", "-c", config_file_path("deploy_with_accessories"))

assert_nil ENV["KAMAL_MESSAGE"]
end
end
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