The foreachdirectory command executes commands in each directory found by traversing the filesystem. This is particularly useful for monorepos or multi-module projects.
Each item found is made available to child commands via the ITEM environment variable, which contains the relative path of the current directory being processed.
Attributes
Required
type: "foreachdirectory": Identifies this as a foreach directory command
name: Descriptive name for the command
mode: Execution mode (parallel or serial)
depth: Directory traversal depth (0 for unlimited, 1 for immediate children only)
include_hidden: Whether to include hidden directories (true or false)
working_directory_strategy: How to set working directory (none, item_relative)
Optional
working_directory: Base directory to start traversal from
env: Environment variables inherited by all child commands
runs_on_condition: When to run (success, error, always, exit-codes)
runs_on_exit_codes: Specific exit codes that trigger execution
commands: List of commands to execute in each directory (either this or command_group)
command_group: Reference to a named command group (either this or commands)
- type: "foreachdirectory"name: "Serial Module Builds"working_directory: "./packages"mode: "serial"depth: 1include_hidden: falseworking_directory_strategy: "item_relative"commands:
- type: "shell"name: "Build Package"command_line: "npm run build"# Packages built one after another
Depth Control
Depth 1 - Immediate Children Only
- type: "foreachdirectory"name: "Top Level Only"working_directory: "./src"depth: 1include_hidden: falsemode: "parallel"working_directory_strategy: "item_relative"commands:
- type: "shell"name: "Process"command_line: "process.sh"# Only processes ./src/dir1, ./src/dir2# Does NOT process ./src/dir1/subdir
Depth 0 - Unlimited Recursion
- type: "foreachdirectory"name: "All Directories"working_directory: "./src"depth: 0include_hidden: falsemode: "parallel"working_directory_strategy: "item_relative"commands:
- type: "shell"name: "Find Go Modules"command_line: "test -f go.mod && go test ./..."# Processes all directories at any depth
Complete Example
name: "Monorepo Testing"description: "Test all modules in a monorepo structure"commands:
- type: "foreachdirectory"name: "Test All Modules"working_directory: "./modules"mode: "parallel"depth: 1include_hidden: falseworking_directory_strategy: "item_relative"commands:
- type: "shell"name: "Check for Tests"command_line: | if [ ! -d ./tests ]; then
echo "No tests found in $(pwd)" 1>&2
exit 99
fiskip_exit_codes: [99]
- type: "shell"name: "Install Dependencies"command_line: "npm install" - type: "shell"name: "Run Tests"command_line: "npm test" - type: "shell"name: "Build Module"command_line: "npm run build"
Skip Pattern
Use skip codes to skip directories without certain criteria:
- type: "foreachdirectory"name: "Build Go Modules"working_directory: "./services"mode: "parallel"depth: 1include_hidden: falseworking_directory_strategy: "item_relative"commands:
- type: "shell"name: "Check for go.mod"command_line: | if [ ! -f go.mod ]; then
echo "No go.mod in $ITEM, skipping" 1>&2
exit 100
fiskip_exit_codes: [100]
- type: "shell"name: "Build"command_line: "go build ./..."
Hidden Directories
Control whether hidden directories (starting with .) are included: