Exec
Execute shell commands as Alchemy resources with support for environment variables, secrets, working directories, and memoization.Properties
The command to execute (including any arguments).
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:- The command string changes, or
- The contents of any files matching the glob patterns change
falseWorking directory for the command.
Environment variables to set. Supports Secret values for sensitive data.
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: trueReturns
Unique identifier for this execution.
Exit code of the command.
Standard output from the command (only available when
inheritStdio is false).Standard error from the command (only available when
inheritStdio is false).Timestamp when the command was executed.
Whether the command has completed execution.
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 theExec resource, this is a one-time execution that doesn’t persist state.
Syntax
Parameters
The command to execute.
Options for the command execution:
captureOutput: Whether to capture stdout and stderr (default:false)cwd: Working directoryenv: Environment variables- All Node.js
SpawnOptions
Returns
IfcaptureOutput 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
- Use for expensive operations: Memoize commands that are slow or resource-intensive (builds, tests, etc.)
- Track the right files: Include all files that affect the command output in your patterns
- Disable in CI: Prevent memoization in CI to ensure fresh builds
- Only memoize idempotent commands: Commands that can be safely run multiple times with the same result
Security Best Practices
- Always use Secrets for sensitive data:
- Validate command strings: Be careful with user input in commands