Skip to main content

Exec

Execute shell commands as Alchemy resources with support for environment variables, secrets, working directories, and memoization.

Properties

string
required
The command to execute (including any arguments).
boolean | { patterns: string[] }
Whether to memoize the command (only re-run if the command changes).When set to true, the command will only be re-executed if the command string changes.When set to an object with patterns, the command will be re-executed if either:
  1. The command string changes, or
  2. The contents of any files matching the glob patterns change
Important: When using memoization with build commands, the build outputs will not be produced if the command is memoized. Consider disabling memoization in CI environments.Default: false
string
Working directory for the command.
Record<string, string | Secret<string> | undefined>
Environment variables to set. Supports Secret values for sensitive data.
boolean
Whether to inherit stdio from parent process. When true, command output is displayed in real-time. When false, output is captured and available in the return value.Default: true

Returns

string
Unique identifier for this execution.
number
Exit code of the command.
string
Standard output from the command (only available when inheritStdio is false).
string
Standard error from the command (only available when inheritStdio is false).
number
Timestamp when the command was executed.
boolean
Whether the command has completed execution.
string
Hash of the command inputs (when using memoization with patterns).

Examples

Run a simple command

Capture command output

Run command in specific directory

Use secrets in environment

Memoize a command

Memoize with file patterns

Disable memoization in CI

Database migration example


exec() Function

Lower-level function to execute shell commands. Unlike the Exec resource, this is a one-time execution that doesn’t persist state.

Syntax

Parameters

string
required
The command to execute.
ExecOptions
Options for the command execution:
  • captureOutput: Whether to capture stdout and stderr (default: false)
  • cwd: Working directory
  • env: Environment variables
  • All Node.js SpawnOptions

Returns

If captureOutput is true, returns { stdout: string, stderr: string }. Otherwise, returns undefined.

Examples

Execute with inherited stdio

Capture output

Custom working directory and environment


Best Practices

Memoization Guidelines

  1. Use for expensive operations: Memoize commands that are slow or resource-intensive (builds, tests, etc.)
  2. Track the right files: Include all files that affect the command output in your patterns
  1. Disable in CI: Prevent memoization in CI to ensure fresh builds
  1. Only memoize idempotent commands: Commands that can be safely run multiple times with the same result

Security Best Practices

  1. Always use Secrets for sensitive data:
  1. Validate command strings: Be careful with user input in commands