22
33set -euo pipefail
44
5+ # Auto-detect which Terraform modules to validate based on changed files from paths-filter
6+ # Uses paths-filter outputs from GitHub Actions:
7+ # ALL_CHANGED_FILES - all files changed in the PR (for logging)
8+ # SHARED_CHANGED - boolean indicating if shared infrastructure changed
9+ # MODULE_CHANGED_FILES - only files in registry/**/modules/** (for processing)
10+ # Validates all modules if shared infrastructure changes or if env vars are not set (local dev)
11+ #
12+ # This script only validates changed modules. Documentation and template changes are ignored.
13+
514validate_terraform_directory () {
615 local dir=" $1 "
716 echo " Running \` terraform validate\` in $dir "
@@ -12,18 +21,58 @@ validate_terraform_directory() {
1221}
1322
1423main () {
15- # Get the directory of the script
16- local script_dir=$( dirname " $( readlink -f " $0 " ) " )
24+ echo " ==> Detecting changed files..."
25+
26+ if [[ -n " ${ALL_CHANGED_FILES:- } " ]]; then
27+ echo " Changed files in PR:"
28+ echo " $ALL_CHANGED_FILES " | tr ' ' ' \n' | sed ' s/^/ - /'
29+ echo " "
30+ fi
1731
18- # Code assumes that registry directory will always be in same position
19- # relative to the main script directory
32+ local script_dir=$( dirname " $( readlink -f " $0 " ) " )
2033 local registry_dir=" $script_dir /../registry"
2134
22- # Get all module subdirectories in the registry directory. Code assumes that
23- # Terraform module directories won't begin to appear until three levels deep into
24- # the registry (e.g., registry/coder/modules/coder-login, which will then
25- # have a main.tf file inside it)
26- local subdirs=$( find " $registry_dir " -mindepth 3 -path " */modules/*" -type d | sort)
35+ if [[ " ${SHARED_CHANGED:- false} " == " true" ]]; then
36+ echo " ==> Shared infrastructure changed"
37+ echo " ==> Validating all modules for safety"
38+ local subdirs=$( find " $registry_dir " -mindepth 3 -path " */modules/*" -type d | sort)
39+ elif [[ -z " ${MODULE_CHANGED_FILES:- } " ]]; then
40+ echo " ✓ No module files changed, skipping validation"
41+ exit 0
42+ else
43+ CHANGED_FILES=$( echo " $MODULE_CHANGED_FILES " | tr ' ' ' \n' )
44+
45+ MODULE_DIRS=()
46+ while IFS= read -r file; do
47+ if [[ " $file " =~ \. (md| png| jpg| jpeg| svg)$ ]]; then
48+ continue
49+ fi
50+
51+ if [[ " $file " =~ ^registry/([^/]+)/modules/([^/]+)/ ]]; then
52+ namespace=" ${BASH_REMATCH[1]} "
53+ module=" ${BASH_REMATCH[2]} "
54+ module_dir=" registry/${namespace} /modules/${module} "
55+
56+ if [[ -d " $module_dir " ]] && [[ ! " ${MODULE_DIRS[*]} " =~ " ${module_dir} " ]]; then
57+ MODULE_DIRS+=(" $module_dir " )
58+ fi
59+ fi
60+ done <<< " $CHANGED_FILES"
61+
62+ if [[ ${# MODULE_DIRS[@]} -eq 0 ]]; then
63+ echo " ✓ No modules to validate"
64+ echo " (documentation, templates, namespace files, or modules without changes)"
65+ exit 0
66+ fi
67+
68+ echo " ==> Validating ${# MODULE_DIRS[@]} changed module(s):"
69+ for dir in " ${MODULE_DIRS[@]} " ; do
70+ echo " - $dir "
71+ done
72+ echo " "
73+
74+ local subdirs=" ${MODULE_DIRS[*]} "
75+ fi
2776
2877 for dir in $subdirs ; do
2978 # Skip over any directories that obviously don't have the necessary
0 commit comments