|
2 | 2 |
|
3 | 3 | set -euo pipefail |
4 | 4 |
|
| 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 skips if no changes detected |
| 11 | +# |
| 12 | +# This script only validates changed modules. Documentation and template changes are ignored. |
| 13 | + |
5 | 14 | validate_terraform_directory() { |
6 | 15 | local dir="$1" |
7 | 16 | echo "Running \`terraform validate\` in $dir" |
8 | | - pushd "$dir" |
| 17 | + pushd "$dir" > /dev/null |
9 | 18 | terraform init -upgrade |
10 | 19 | terraform validate |
11 | | - popd |
| 20 | + popd > /dev/null |
12 | 21 | } |
13 | 22 |
|
14 | 23 | main() { |
15 | | - # Get the directory of the script |
| 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 |
| 31 | + |
16 | 32 | local script_dir=$(dirname "$(readlink -f "$0")") |
| 33 | + local registry_dir=$(readlink -f "$script_dir/../registry") |
| 34 | + |
| 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 -maxdepth 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 |
17 | 50 |
|
18 | | - # Code assumes that registry directory will always be in same position |
19 | | - # relative to the main script directory |
20 | | - local registry_dir="$script_dir/../registry" |
| 51 | + if [[ "$file" =~ ^registry/([^/]+)/modules/([^/]+)/ ]]; then |
| 52 | + namespace="${BASH_REMATCH[1]}" |
| 53 | + module="${BASH_REMATCH[2]}" |
| 54 | + module_dir="registry/${namespace}/modules/${module}" |
21 | 55 |
|
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) |
| 56 | + if [[ -d "$module_dir" ]] && [[ ! " ${MODULE_DIRS[*]} " =~ " ${module_dir} " ]]; then |
| 57 | + MODULE_DIRS+=("$module_dir") |
| 58 | + fi |
| 59 | + fi |
| 60 | + done <<< "$CHANGED_FILES" |
27 | 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 |
| 76 | + |
| 77 | + status=0 |
28 | 78 | for dir in $subdirs; do |
29 | 79 | # Skip over any directories that obviously don't have the necessary |
30 | 80 | # files |
31 | 81 | if test -f "$dir/main.tf"; then |
32 | | - validate_terraform_directory "$dir" |
| 82 | + if ! validate_terraform_directory "$dir"; then |
| 83 | + status=1 |
| 84 | + fi |
33 | 85 | fi |
34 | 86 | done |
| 87 | + |
| 88 | + exit $status |
35 | 89 | } |
36 | 90 |
|
37 | 91 | main |
0 commit comments