Skip to content

Let hooks talk back to Kamal via KAMAL_OUTPUT - #1782

Open
jeremy wants to merge 1 commit into
mainfrom
hook-output
Open

jeremy wants to merge 1 commit into
mainfrom
hook-output

Conversation

@jeremy

@jeremy jeremy commented Feb 20, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Hooks can now pass data back to Kamal by writing KEY=VALUE lines (dotenv format) to the file at $KAMAL_OUTPUT
  • Special key KAMAL_MESSAGE is printed to the user after the hook completes
  • Hook outputs accumulate across the deploy lifecycle — a pre-deploy hook that writes DEPLOY_ID=123 makes $DEPLOY_ID available to post-deploy
  • Precedence: secrets > built-in KAMAL_* tags > hook outputs
  • Backward-compatible: hooks that don't write to $KAMAL_OUTPUT behave identically to before

Files

  • lib/kamal/hook_output.rb — new: tempfile lifecycle + dotenv parsing
  • lib/kamal/cli/base.rb — run_hook creates HookOutput, passes KAMAL_OUTPUT, parses output, prints KAMAL_MESSAGE, cleans up in ensure
  • lib/kamal/commander.rb — hook_outputs accumulator, merge_hook_output
  • lib/kamal/commands/hook.rb — merge accumulated hook outputs into hook env with correct precedence
  • Sample hooks updated to list KAMAL_OUTPUT

Test plan

  • test/hook_output_test.rb — 7 tests: parse, cleanup, empty file, quoted values, path access
  • test/commander_test.rb — 3 new tests: starts empty, accumulates, overwrites
  • test/commands/hook_test.rb — 3 new tests: env with hook_outputs, cannot override tags, cannot override secrets
  • Existing hook tests pass unchanged

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6e7c0f2de3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/kamal/hook_output.rb Outdated

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

This PR adds a mechanism for hooks to pass data back to Kamal by writing KEY=VALUE pairs to a file specified by the $KAMAL_OUTPUT environment variable. Hook outputs accumulate across the deploy lifecycle and are made available to subsequent hooks through environment variables, with a proper precedence order ensuring secrets and built-in tags cannot be overridden.

Changes:

  • New Kamal::HookOutput class manages temporary file lifecycle and parses dotenv-formatted output
  • Hook outputs accumulate in Commander#hook_outputs and are passed to subsequent hooks via Commands::Hook#env
  • Special KAMAL_MESSAGE key is displayed to users after hook completion

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/kamal/hook_output.rb New class for managing hook output tempfile and parsing dotenv format
lib/kamal/cli/base.rb Creates HookOutput, passes path to hooks, parses output, displays messages, ensures cleanup
lib/kamal/commander.rb Adds hook_outputs accumulator and merge_hook_output method
lib/kamal/commands/hook.rb Merges accumulated hook outputs into hook environment with correct precedence (secrets > tags > hook_outputs)
lib/kamal/cli/templates/sample_hooks/*.sample Documents KAMAL_OUTPUT environment variable availability
test/hook_output_test.rb Comprehensive unit tests for HookOutput parsing and cleanup
test/commands/hook_test.rb Tests for hook environment with hook_outputs and precedence rules
test/commander_test.rb Tests for hook_outputs accumulation behavior

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

Comment thread lib/kamal/cli/base.rb
Comment thread lib/kamal/cli/templates/sample_hooks/pre-deploy.sample
Hooks are fire-and-forget today: exit code is the only feedback channel.
This adds a file-based output channel so hooks can set variables that
Kamal reads back.

Before each hook, Kamal creates a tempfile and passes its path as
KAMAL_OUTPUT. The hook writes dotenv-format KEY=VALUE lines. Kamal
parses them after the hook exits and accumulates them on Commander,
merging into the env for subsequent hooks. A pre-deploy hook that
writes DEPLOY_ID=123 makes $DEPLOY_ID available to post-deploy.

KAMAL_MESSAGE is special: if present, Kamal prints it regardless of
verbosity settings, giving hooks a dedicated user-facing output channel
separate from stdout logging.

Backward-compatible: hooks that don't write to KAMAL_OUTPUT behave
identically to today.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-06T04:57:28.172120Z 7583a23 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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