Skip to content

fix(terragrunt_* hooks): Use new subcommands for terragrunt v0.78.0+ instead of deprecated ones #901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 29, 2025
32 changes: 32 additions & 0 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,35 @@ function common::export_provided_env_vars {
export $var_name="$var_value"
done
}

#######################################################################
# Check if the installed Terragrunt version is >=0.78.0 or not
#
# This function helps to determine which terragrunt subcomand to use
# based on Terragrunt version
#
# Returns:
# - 0 if version >= 0.78.0
# - 1 if version < 0.78.0
# Defaults to 0 if version cannot be determined
#######################################################################
# TODO: Drop after May 2027. Two years to upgrade is more than enough.
function common::terragrunt_version_ge_0.78 {
local terragrunt_version

# Extract version number (e.g., "terragrunt version v0.80.4" -> "0.80")
terragrunt_version=$(terragrunt --version 2> /dev/null | grep -oE '[0-9]+\.[0-9]+')
# If we can't parse version, default to newer command
[[ ! $terragrunt_version ]] && return 0
fi

local major minor
IFS='.' read -r major minor <<< "$terragrunt_version"

# New subcommands added in v0.78.0 (May 2025)
if [[ $major -gt 0 || ($major -eq 0 && $minor -ge 78) ]]; then
return 0
else
return 1
fi
}
9 changes: 6 additions & 3 deletions hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ function main {
common::parse_cmdline "$@"
common::export_provided_env_vars "${ENV_VARS[@]}"
common::parse_and_export_env_vars
# JFYI: terragrunt hclfmt color already suppressed via PRE_COMMIT_COLOR=never
# JFYI: `terragrunt hcl format` color already suppressed via PRE_COMMIT_COLOR=never

readonly SUBCOMMAND
common::terragrunt_version_ge_0.78 && SUBCOMMAND=(hcl format) || SUBCOMMAND=(hclfmt)

# shellcheck disable=SC2153 # False positive
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
Expand Down Expand Up @@ -46,7 +49,7 @@ function per_dir_hook_unique_part {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt hclfmt "${args[@]}"
terragrunt "${SUBCOMMAND[@]}" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand All @@ -63,7 +66,7 @@ function run_hook_on_whole_repo {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt hclfmt "$(pwd)" "${args[@]}"
terragrunt "${SUBCOMMAND[@]}" "$(pwd)" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand Down
9 changes: 8 additions & 1 deletion hooks/terragrunt_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ function main {
common::parse_and_export_env_vars
# JFYI: terragrunt providers lock color already suppressed via PRE_COMMIT_COLOR=never

readonly RUN_ALL_SUBCOMMAND
if common::terragrunt_version_ge_0.78; then
RUN_ALL_SUBCOMMAND=(run --all providers lock)
else
RUN_ALL_SUBCOMMAND=(run-all providers lock)
fi

# shellcheck disable=SC2153 # False positive
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
}
Expand Down Expand Up @@ -63,7 +70,7 @@ function run_hook_on_whole_repo {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt run-all providers lock "${args[@]}"
terragrunt "${RUN_ALL_SUBCOMMAND[@]}" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand Down
9 changes: 8 additions & 1 deletion hooks/terragrunt_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ function main {
common::parse_and_export_env_vars
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never

readonly RUN_ALL_SUBCOMMAND
if common::terragrunt_version_ge_0.78; then
RUN_ALL_SUBCOMMAND=(run --all validate)
else
RUN_ALL_SUBCOMMAND=(run-all validate)
fi

# shellcheck disable=SC2153 # False positive
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
}
Expand Down Expand Up @@ -63,7 +70,7 @@ function run_hook_on_whole_repo {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt run-all validate "${args[@]}"
terragrunt "${RUN_ALL_SUBCOMMAND[@]}" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand Down
14 changes: 12 additions & 2 deletions hooks/terragrunt_validate_inputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ function main {
common::parse_and_export_env_vars
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never

readonly SUBCOMMAND
readonly RUN_ALL_SUBCOMMAND
if common::terragrunt_version_ge_0.78; then
SUBCOMMAND=(hcl validate --inputs)
RUN_ALL_SUBCOMMAND=(run --all hcl validate --inputs)
else
SUBCOMMAND=(validate-inputs)
RUN_ALL_SUBCOMMAND=(run-all validate-inputs)
fi

# shellcheck disable=SC2153 # False positive
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
}
Expand Down Expand Up @@ -46,7 +56,7 @@ function per_dir_hook_unique_part {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt validate-inputs "${args[@]}"
terragrunt "${SUBCOMMAND[@]}" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand All @@ -63,7 +73,7 @@ function run_hook_on_whole_repo {
local -a -r args=("$@")

# pass the arguments to hook
terragrunt run-all validate-inputs "${args[@]}"
terragrunt "${RUN_ALL_SUBCOMMAND[@]}" "${args[@]}"

# return exit code to common::per_dir_hook
local exit_code=$?
Expand Down