Skip to content

Commit 49e808d

Browse files
committed
ci: adding changes to the validate process
feat: GCP budget alert building block ci: adding changes to the validate process feat: GCP budget alert building block
1 parent 5da10ad commit 49e808d

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

.github/workflows/.validate-modules.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

.github/workflows/build.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name: build
22
on:
33
push:
4-
branches: [ main ]
4+
branches: [ main, feature/* ]
55
pull_request:
66
branches: [ main ]
77
merge_group:
88

9+
910
jobs:
1011
pre-commit:
1112
runs-on: ubuntu-latest
@@ -24,3 +25,6 @@ jobs:
2425

2526
- name: ensure all pre-commit hooks pass
2627
run: pre-commit run --all-files --show-diff-on-failure
28+
29+
- name: Run validation script
30+
run: ci/validate_modules.sh

ci/validate_modules.sh

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,137 @@
11
#!/bin/bash
22

3+
# CLI output colors
4+
RED='\033[0;31m'
5+
YELLOW='\033[1;33m'
6+
NC='\033[0m' # No Color
7+
38
errors=()
9+
warnings=()
10+
11+
# Check if Terraform is installed
12+
if ! command -v terraform >/dev/null 2>&1; then
13+
echo -e "${RED}Error:${NC} Terraform is not installed or not in PATH"
14+
exit 1
15+
fi
416

517
check_readme_format() {
618
local readme_path="$1"
19+
720
if [[ ! -f "$readme_path" ]]; then
821
errors+=("README.md not found at $readme_path")
922
return 1
1023
fi
1124

12-
if ! grep -q "^---" "$readme_path"; then
13-
errors+=("Missing '---' at the beginning of the README.md at $readme_path")
14-
fi
15-
if ! grep -q "name: " "$readme_path"; then
16-
errors+=("Missing 'name' in README.md at $readme_path")
25+
# Check for valid YAML front matter
26+
if ! awk '/^---/{f=1; next} /^---$/{if(f){exit 0}} END{exit 1}' "$readme_path"; then
27+
errors+=("README.md at $readme_path does not have a valid YAML front matter block (start and end with '---')")
28+
return 1
1729
fi
18-
if ! grep -q "supportedPlatforms:" "$readme_path"; then
19-
errors+=("Missing 'supportedPlatforms' in README.md at $readme_path")
30+
31+
# Extract YAML block
32+
local yaml_header
33+
yaml_header=$(awk '/^---/{f=1; next} /^---$/{f=0} f' "$readme_path")
34+
35+
# Check for required fields
36+
if ! grep -q "name:" <<< "$yaml_header"; then
37+
errors+=("Missing 'name' in YAML header of README.md at $readme_path")
2038
fi
21-
if ! grep -q "description:" "$readme_path"; then
22-
errors+=("Missing 'description' in README.md at $readme_path")
39+
if ! grep -q "supportedPlatforms:" <<< "$yaml_header"; then
40+
errors+=("Missing 'supportedPlatforms' in YAML header of README.md at $readme_path")
2341
fi
24-
if ! grep -q "^---$" "$readme_path"; then
25-
errors+=("Missing ending '---' in README.md at $readme_path")
42+
if ! grep -q "description:" <<< "$yaml_header"; then
43+
errors+=("Missing 'description' in YAML header of README.md at $readme_path")
2644
fi
45+
2746
return 0
2847
}
2948

3049
check_png_naming() {
3150
local png_path="$1"
32-
local png_name=$(basename "$png_path")
51+
local png_name
52+
png_name=$(basename "$png_path")
53+
3354
if [[ "$png_name" != "logo.png" ]]; then
34-
errors+=("Warning: PNG file '$png_name' should be named 'logo.png' to be importable in meshStack")
55+
warnings+=("PNG file '$png_name' should be named 'logo.png' to be importable in meshStack (path: $png_path)")
3556
fi
3657
}
3758

3859
check_terraform_files() {
3960
local buildingblock_path="$1"
40-
local tf_files=("main.tf" "variables.tf" "outputs.tf")
4161

42-
for tf_file in "${tf_files[@]}"; do
62+
# Check if any .tf files exist
63+
if ! find "$buildingblock_path" -maxdepth 1 -name '*.tf' | grep -q .; then
64+
errors+=("No Terraform (.tf) files found in $buildingblock_path")
65+
return 1
66+
fi
67+
68+
# Optional: Check for recommended files
69+
local required_tf_files=("main.tf" "variables.tf" "outputs.tf" "provider.tf" "versions.tf")
70+
for tf_file in "${required_tf_files[@]}"; do
4371
if [[ ! -f "$buildingblock_path/$tf_file" ]]; then
44-
errors+=("Error: '$tf_file' not found in $buildingblock_path")
72+
warnings+=("Recommended file '$tf_file' is missing in $buildingblock_path")
4573
fi
4674
done
47-
return 0
75+
76+
# Validate Terraform configuration
77+
pushd "$buildingblock_path" > /dev/null || return 1
78+
79+
if ! terraform init -backend=false -input=false > /dev/null 2>&1; then
80+
errors+=("Terraform init failed in $buildingblock_path")
81+
elif ! terraform validate > /dev/null 2>&1; then
82+
errors+=("Terraform validate failed in $buildingblock_path")
83+
fi
84+
85+
popd > /dev/null || return 1
4886
}
4987

50-
# Ensure it is called from the repo root
51-
cd "$(dirname "$0")/.."
88+
# Ensure the script is run from repo root
89+
cd "$(dirname "$0")/.." || exit 1
5290
modules_path="modules"
5391

5492
if [[ ! -d "$modules_path" ]]; then
55-
echo "Error: Modules folder not found at $modules_path"
93+
echo -e "${RED}Error:${NC} Modules folder not found at $modules_path"
5694
exit 1
5795
fi
5896

5997
modules_glob="$modules_path/*/*/buildingblock"
6098

61-
for readme_file in $(find $modules_glob -name 'README.md'); do
99+
# Check all README.md files
100+
find $modules_glob -name 'README.md' -print0 | while IFS= read -r -d '' readme_file; do
62101
check_readme_format "$readme_file"
63102
done
64103

65-
for png_file in $(find $modules_glob -name '*.png'); do
104+
# Check all PNG files
105+
find $modules_glob -name '*.png' -print0 | while IFS= read -r -d '' png_file; do
66106
check_png_naming "$png_file"
67107
done
68108

69-
for buildingblock_dir in $(find $modules_glob -type d -name 'buildingblock'); do
109+
# Check all Terraform buildingblock directories
110+
find $modules_glob -type d -name 'buildingblock' -print0 | while IFS= read -r -d '' buildingblock_dir; do
70111
check_terraform_files "$buildingblock_dir"
71112
done
72113

114+
# Print results
115+
echo ""
73116
echo "Number of errors: ${#errors[@]}"
117+
echo "Number of warnings: ${#warnings[@]}"
118+
echo ""
119+
74120
if [[ ${#errors[@]} -gt 0 ]]; then
75-
echo "Errors found:"
121+
echo -e "${RED}Errors found:${NC}"
76122
for error in "${errors[@]}"; do
77-
echo "- $error"
123+
echo -e "- $error"
78124
done
125+
echo ""
79126
exit 1
127+
elif [[ ${#warnings[@]} -gt 0 ]]; then
128+
echo -e "${YELLOW}Warnings found:${NC}"
129+
for warning in "${warnings[@]}"; do
130+
echo -e "- $warning"
131+
done
132+
echo ""
133+
exit 0
80134
else
81-
echo "All checks passed successfully."
135+
echo "All checks passed successfully."
82136
exit 0
83137
fi

0 commit comments

Comments
 (0)