docs/src/data/experiments/hook-context-env.mdx
Expose additional TG_CTX_* environment variables to hook scripts.
hook-context-env - What it doesWhen enabled, Terragrunt sets three additional environment variables on the process running every before_hook, after_hook, and error_hook:
TG_CTX_HOOK_TYPE — before_hook, after_hook, or error_hook, depending on which lifecycle phase is executing the hook.TG_CTX_SOURCE — the resolved terraform source URL for the current unit, matching the precedence Terragrunt uses for the actual download: the --source CLI override if set, otherwise the evaluated terraform.source (with --source-map applied), otherwise ..TG_CTX_TERRAGRUNT_DIR — the directory containing the current Terragrunt config (equivalent to get_terragrunt_dir()).These variables make it easier for hook scripts to branch on lifecycle phase, to know whether the unit pulls remote source, and to locate the config directory without having to thread that information through hook arguments.
terragrunt run --all --experiment hook-context-env -- apply
Example hook script:
#!/usr/bin/env bash
case "$TG_CTX_HOOK_TYPE" in
before_hook) echo "preparing $TG_CTX_TERRAGRUNT_DIR" ;;
after_hook) echo "cleaning up $TG_CTX_TERRAGRUNT_DIR" ;;
error_hook) echo "failure in $TG_CTX_TERRAGRUNT_DIR" ;;
esac
if [ "$TG_CTX_SOURCE" != "." ]; then
echo "unit uses source: $TG_CTX_SOURCE"
fi
hook-context-env - How to provide feedbackProvide your feedback in the hook-context-env GitHub Discussion.
hook-context-env - Criteria for stabilizationTo transition the hook-context-env feature to a stable release, the following must be addressed:
before_hook/after_hook/error_hook) match user expectations.