Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal

Deploy web apps anywhere.

View on GitHub

17 documents · Updated Nov 16, 2026

docs / command-execution

Command execution and orchestration

Kamal's command system turns a CLI request into local or remote deployment work. Thor selects a command, the shared commander prepares configuration and target scope, and the command layer builds the Docker, SSH, and file actions that SSHKit runs. This page gives the shared model; see application deployment, accessory operations, proxy management, image building, and host operations for the main work areas.

Command dispatch

The CLI is the entry point for user intent. bin/kamal starts Kamal::Cli::Main, and the main class exposes top-level actions and subcommands for application, accessory, build, proxy, server, registry, lock, and prune work. A top-level action can invoke child actions to form a larger workflow. For example, deployment coordinates image delivery, proxy boot, application boot, and cleanup.

The dispatch path keeps policy in the CLI layer and command construction in the command layer:

StageResponsibilityResult
CLI dispatchMatch the command and parse shared flagsA selected operation with user scope and options
Commander stateLoad configuration and resolve hosts and rolesA shared deployment context
CLI orchestrationOrder work, hooks, locks, and outputA controlled workflow
Command objectsExpress Docker, SSH, registry, and file actionsCommands for local or remote execution
SSHKitRun actions on selected hosts or locallyResults and command errors

This split lets one workflow combine several areas without making each command know how to parse the full CLI or manage global state.

References: bin/kamal• L6-17 lib/…/main.rb• L236-261 lib/…/base.rb• L5-16

Commander context

Kamal::Commander is the shared context for one CLI run. It lazily loads the deployment configuration, creates command objects, and exposes the selected hosts, roles, primary targets, application hosts, proxy hosts, and accessory hosts. The CLI base class fills this context from shared options such as configuration file, destination, version, host, role, and primary-host selection.

Host and role filters change the target set before work starts. Commands then use that set to run across all matching targets, one primary target, or a narrower user-selected group. The commander also holds cross-cutting state for verbosity, connection setup, deploy-lock ownership, and output logging.

References: lib/…/commander.rb• L7-15 lib/…/commander.rb• L31-70 lib/…/commander.rb• L84-126 lib/…/base.rb• L17-75

Command layers

The command layer provides the shared vocabulary for work on the deployment targets. A command object receives the loaded configuration and returns an action such as a Docker command, an SSH command, a file transfer, or a shell pipeline. The CLI remains responsible for when to run that action and on which hosts.

Kamal::Commands::Base supplies common command forms and SSH settings. Higher-level command objects represent the target being managed. The app and accessory commands describe container work, the proxy command describes the traffic entry point, the builder command selects a build target, and the server and lock commands support host setup and safe coordination.

References: lib/…/base.rb• L1-15 lib/…/base.rb• L17-41 lib/…/base.rb• L43-101 lib/…/commander.rb• L84-126

Remote execution

Remote execution uses SSHKit to apply command objects to a host set. The shared on path runs a connection hook once before the first remote action, then the selected command runs on each host. A command can capture output for display, execute without returning output, or use SSH directly for an interactive session.

Local work uses the same execution interface with run_locally. This supports local image builds, local registry setup, hooks, and interactive commands while keeping the output and error path consistent with remote work.

The execution model is:

flowchart LR
    A[CLI request] --> B[Commander context]
    B --> C[Target selection]
    C --> D{Execution site}
    D -->|Local| E[SSHKit local runner]
    D -->|Remote| F[SSHKit host runner]
    E --> G[Result or error]
    F --> G

References: lib/…/base.rb• L256-267 lib/…/base.rb• L315-327 lib/…/base.rb• L13-15 lib/…/base.rb• L103-133

Workflow coordination

Mutating workflows use a common coordination envelope. modify records the command and target context for output and notifications. When a workflow needs exclusive access, it acquires the deploy lock before remote changes and releases it after the work completes. Nested calls can reuse the lock, so a top-level workflow can safely invoke child commands.

Hooks extend the workflow at named points. They run locally with deployment details and can receive secrets when the workflow allows them. The pre-connect hook runs once before the first remote connection. A failed hook becomes a CLI hook error, while a failed remote action is reported through the SSHKit error path.

sequenceDiagram
    participant User
    participant CLI as CLI layer
    participant C as Commander
    participant L as Lock and hooks
    participant S as SSHKit
    participant H as Remote hosts

    User->>CLI: Run a mutating command
    CLI->>C: Load config and target scope
    CLI->>L: Run pre-work hooks and acquire lock
    CLI->>S: Dispatch command actions
    S->>H: Execute Docker or host work
    H-->>S: Return status and output
    S-->>CLI: Success or error
    CLI->>L: Run post-work hooks and release lock

References: lib/…/base.rb• L78-123 lib/…/base.rb• L135-221 lib/…/base.rb• L224-253 lib/…/commander.rb• L141-163

Deployment composition

The top-level deployment commands show how the layers compose. setup first bootstraps Docker and then performs deployment with accessories. deploy delivers or pulls the app image, runs the pre-deploy hook, ensures the proxy is running, boots the app, and prunes old resources. redeploy uses the existing host setup and proxy. rollback changes the selected app version and boots it only when that version exists on the hosts.

The child pages describe each work area in detail:

References: lib/…/main.rb• L1-54 lib/…/main.rb• L56-110 lib/…/main.rb• L187-228

Output and failure handling

The command system treats output as part of the execution contract. Verbosity can be changed for normal, detailed, quiet, or raw output. Remote command output can be grouped by host, while audit commands record important changes without mixing those records into normal user output.

The executable catches SSHKit runner failures and other uncaught errors at the CLI boundary. It prints a short error by default and adds a backtrace when verbose mode is active. This gives users one failure surface even when a workflow has several nested command calls.

References: bin/kamal• L8-17 lib/…/base.rb• L8-17 lib/…/base.rb• L93-102 lib/…/base.rb• L224-251 lib/…/commander.rb• L165-216

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep shared CLI options aligned with commander state. A new host, role, version, lock, or output option must reach every subcommand that uses it.
  • Preserve invocation tracking when a command invokes another command. It supplies the command and subcommand names used by logs, hooks, and change notifications.
  • Preserve nested lock behavior. A child operation must reuse an existing lock and must not release a lock owned by its parent workflow.
  • Keep local and remote execution paths distinct for interactive and raw output. Mixing them can corrupt output or change the expected terminal behavior.
  • Keep command objects independent of CLI ordering. Changes to Docker or SSH command forms affect every workflow that calls the command object.

Important failure modes

TriggerConsequenceGuard
No selected host or role matches a filterThe command stops before remote work beginsCommander validates filtered hosts and roles and raises an argument error
A deploy lock is already heldA mutating command fails or waits until its configured timeoutThe CLI checks lock state and supports bounded polling with --lock-wait
A remote action failsThe workflow stops and the CLI reports the SSHKit errorThe executable handles runner errors at one boundary; verbose mode exposes the backtrace
A hook exits with failureThe workflow raises a hook-specific errorHook execution wraps the command failure and honors --skip-hooks
Raw output is combined with an interactive or detached operationOutput or command semantics become invalidCLI commands reject incompatible option combinations

Key files

FileWhy to read it
bin/kamalShows the process entry point and the final error boundary.
lib/…/main.rbShows top-level workflow composition and subcommand registration.
lib/…/base.rbShows shared option setup, commander initialization, locks, hooks, and execution helpers.
lib/…/commander.rbShows shared configuration, target selection, command factories, and output state.
lib/…/base.rbShows the common contract for building Docker and SSH actions.
lib/…/app.rbShows how a target-specific CLI command coordinates app work across roles and hosts.
lib/…/build.rbShows how local build work and remote image delivery use the same execution model.