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
33 changes: 33 additions & 0 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,36 @@ 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 help 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_above_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
if [[ -z "$terragrunt_version" ]]; then
return 0
fi

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

# `hcl format` support added in v0.78.0 (May 2025)
if [[ $major -gt 0 ]] || [[ $major -eq 0 && $minor -ge 78 ]]; then
return 0
else
return 1
fi
}
12 changes: 9 additions & 3 deletions hooks/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ 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

if common::terragrunt_version_above_0.78; then
readonly SUBCOMMAND="hcl format"
else
readonly SUBCOMMAND="hclfmt"
fi

# shellcheck disable=SC2153 # False positive
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
Expand Down Expand Up @@ -46,7 +52,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 +69,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