Skip to content

Commit 54468bb

Browse files
baiMaxymVlasovrottj006yermulnik
authored
fix(terragrunt_* hooks): Use new subcommands for terragrunt v0.78.0+ instead of deprecated ones (#901)
--------- Co-authored-by: MaxymVlasov <[email protected]> Co-authored-by: Julien Rottenberg <[email protected]> Co-authored-by: George L. Yermulnik <[email protected]>
1 parent dd939dd commit 54468bb

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

hooks/_common.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,34 @@ function common::export_provided_env_vars {
606606
export $var_name="$var_value"
607607
done
608608
}
609+
610+
#######################################################################
611+
# Check if the installed Terragrunt version is >=0.78.0 or not
612+
#
613+
# This function helps to determine which terragrunt subcomand to use
614+
# based on Terragrunt version
615+
#
616+
# Returns:
617+
# - 0 if version >= 0.78.0
618+
# - 1 if version < 0.78.0
619+
# Defaults to 0 if version cannot be determined
620+
#######################################################################
621+
# TODO: Drop after May 2027. Two years to upgrade is more than enough.
622+
function common::terragrunt_version_ge_0.78 {
623+
local terragrunt_version
624+
625+
# Extract version number (e.g., "terragrunt version v0.80.4" -> "0.80")
626+
terragrunt_version=$(terragrunt --version 2> /dev/null | grep -oE '[0-9]+\.[0-9]+')
627+
# If we can't parse version, default to newer command
628+
[[ ! $terragrunt_version ]] && return 0
629+
630+
local major minor
631+
IFS='.' read -r major minor <<< "$terragrunt_version"
632+
633+
# New subcommands added in v0.78.0 (May 2025)
634+
if [[ $major -gt 0 || ($major -eq 0 && $minor -ge 78) ]]; then
635+
return 0
636+
else
637+
return 1
638+
fi
639+
}

hooks/terragrunt_fmt.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ function main {
1212
common::parse_cmdline "$@"
1313
common::export_provided_env_vars "${ENV_VARS[@]}"
1414
common::parse_and_export_env_vars
15-
# JFYI: terragrunt hclfmt color already suppressed via PRE_COMMIT_COLOR=never
15+
# JFYI: `terragrunt hcl format` color already suppressed via PRE_COMMIT_COLOR=never
16+
17+
if common::terragrunt_version_ge_0.78; then
18+
local -ra SUBCOMMAND=(hcl format)
19+
else
20+
local -ra SUBCOMMAND=(hclfmt)
21+
fi
1622

1723
# shellcheck disable=SC2153 # False positive
1824
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
@@ -46,7 +52,7 @@ function per_dir_hook_unique_part {
4652
local -a -r args=("$@")
4753

4854
# pass the arguments to hook
49-
terragrunt hclfmt "${args[@]}"
55+
terragrunt "${SUBCOMMAND[@]}" "${args[@]}"
5056

5157
# return exit code to common::per_dir_hook
5258
local exit_code=$?
@@ -63,7 +69,7 @@ function run_hook_on_whole_repo {
6369
local -a -r args=("$@")
6470

6571
# pass the arguments to hook
66-
terragrunt hclfmt "$(pwd)" "${args[@]}"
72+
terragrunt "${SUBCOMMAND[@]}" "$(pwd)" "${args[@]}"
6773

6874
# return exit code to common::per_dir_hook
6975
local exit_code=$?

hooks/terragrunt_providers_lock.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt providers lock color already suppressed via PRE_COMMIT_COLOR=never
1616

17+
if common::terragrunt_version_ge_0.78; then
18+
local -ra RUN_ALL_SUBCOMMAND=(run --all providers lock)
19+
else
20+
local -ra RUN_ALL_SUBCOMMAND=(run-all providers lock)
21+
fi
22+
1723
# shellcheck disable=SC2153 # False positive
1824
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
1925
}
@@ -63,7 +69,7 @@ function run_hook_on_whole_repo {
6369
local -a -r args=("$@")
6470

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

6874
# return exit code to common::per_dir_hook
6975
local exit_code=$?

hooks/terragrunt_validate.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never
1616

17+
if common::terragrunt_version_ge_0.78; then
18+
local -ra RUN_ALL_SUBCOMMAND=(run --all validate)
19+
else
20+
local -ra RUN_ALL_SUBCOMMAND=(run-all validate)
21+
fi
22+
1723
# shellcheck disable=SC2153 # False positive
1824
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
1925
}
@@ -63,7 +69,7 @@ function run_hook_on_whole_repo {
6369
local -a -r args=("$@")
6470

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

6874
# return exit code to common::per_dir_hook
6975
local exit_code=$?

hooks/terragrunt_validate_inputs.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never
1616

17+
if common::terragrunt_version_ge_0.78; then
18+
local -ra SUBCOMMAND=(hcl validate --inputs)
19+
local -ra RUN_ALL_SUBCOMMAND=(run --all hcl validate --inputs)
20+
else
21+
local -ra SUBCOMMAND=(validate-inputs)
22+
local -ra RUN_ALL_SUBCOMMAND=(run-all validate-inputs)
23+
fi
24+
1725
# shellcheck disable=SC2153 # False positive
1826
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
1927
}
@@ -46,7 +54,7 @@ function per_dir_hook_unique_part {
4654
local -a -r args=("$@")
4755

4856
# pass the arguments to hook
49-
terragrunt validate-inputs "${args[@]}"
57+
terragrunt "${SUBCOMMAND[@]}" "${args[@]}"
5058

5159
# return exit code to common::per_dir_hook
5260
local exit_code=$?
@@ -63,7 +71,7 @@ function run_hook_on_whole_repo {
6371
local -a -r args=("$@")
6472

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

6876
# return exit code to common::per_dir_hook
6977
local exit_code=$?

0 commit comments

Comments
 (0)