Terragrunt hooks receive additional TG_CTX_* environment variables
Introduced 2026-06-10 · score 0.92
HOW TO USE IT
When a shared hook script needs to identify its lifecycle phase or access the unit’s source and config directory, enable `hook-context-env` for the Terragrunt run. Read `TG_CTX_HOOK_TYPE`, `TG_CTX_SOURCE`, and `TG_CTX_TERRAGRUNT_DIR` in the hook script.
terragrunt run --all --experiment hook-context-env -- applyWHY THIS HELPS
When sharing a hook script across Terragrunt lifecycle phases, read the added context variables inside the hook to identify its phase and access the unit source and config directory.
REQUIREMENTS AND LIMITATIONS
- Version: v1.0.8
- Availability: experimental
- --experiment hook-context-env (required): Enables the `hook-context-env` experiment for the run.
- --all (required): Runs the specified OpenTofu/Terraform command across units in the current stack.
- TG_CTX_HOOK_TYPE: Identifies the hook lifecycle phase: `before_hook`, `after_hook`, or `error_hook`.
- TG_CTX_SOURCE: Resolved source URL: CLI `--source` override, otherwise evaluated `terraform.source` with `--source-map` applied, otherwise `.`.
- TG_CTX_TERRAGRUNT_DIR: Directory containing the current Terragrunt config; equivalent to `get_terragrunt_dir()`.
- Prerequisite: Enable the `hook-context-env` experiment for the run; it adds the variables to every `before_hook`, `after_hook`, and `error_hook`.
- The variables are surfaced to every `before_hook`, `after_hook`, and `error_hook`.
- `TG_CTX_HOOK_TYPE` identifies `before_hook`, `after_hook`, or `error_hook`.
- `TG_CTX_SOURCE` is the resolved terraform source URL: CLI `--source` override, otherwise evaluated `terraform.source` with source-map applied, otherwise `.`.
- `TG_CTX_TERRAGRUNT_DIR` is the directory of the current Terragrunt config.
FIRST-PARTY EVIDENCE
- https://github.com/gruntwork-io/terragrunt/releases/tag/v1.0.8
- https://docs.terragrunt.com/reference/experiments/active/
- https://docs.terragrunt.com/reference/cli/commands/run/
Check the installed version and the source before using it. Entry JSON