-
Notifications
You must be signed in to change notification settings - Fork 1
81 lines (68 loc) · 2.33 KB
/
validate-zone.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
name: Validate zone
on:
pull_request:
paths:
- 'dns-records/*.yaml'
jobs:
validate-zone:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate zone
id: validate-zone
run: |
yaml_files=()
error_occurred=false
zone_info='infra-deployment-scripts/zone-info.json'
declare -A valid_zones
# Ensure file exists
if [ ! -f "$zone_info" ]; then
echo "::error::JSON file not found."
exit 1
fi
# Extract zone data from JSON file
valid_zones_data=$(jq -r '.[] | .[] | "\(.["Zone Ref"])=\(.Domain)"' "$zone_info" | tr -d '\r')
# Populate the associative array
while IFS="=" read -r zone_ref domain; do
valid_zones["$zone_ref"]="$domain"
done <<< "$valid_zones_data"
# Find changed YAML files
yaml_files=($(git diff --name-only HEAD^..HEAD | grep -E '^dns-records/.*\.ya?ml$'))
# Check if there are any YAML files to process
if [ ${#yaml_files[@]} -eq 0 ]; then
echo "No YAML files to process after filtering. Exiting cleanly."
exit 0
fi
# Checks if zone is valid and if domain matches zone
validate_file() {
local file=$1
local zone domain
zone=$(yq e '.spec.managedZoneRef.external // ""' "$file")
if [ -z "${valid_zones[$zone]}" ]; then
echo "::error file=$file::Error in file '$file': Invalid zone."
error_occurred=true
else
domain=$(yq e '.spec.name // ""' "$file" | sed 's/^[^.]*\.//; s/\.$//')
if [ "${valid_zones[$zone]}" != "$domain" ]; then
echo "::error file=$file::Error in file '$file': Zone does not match domain."
error_occurred=true
else
echo "Zone validation passed for '$file'."
fi
fi
}
for file in "${yaml_files[@]}"; do
if [ -f "$file" ]; then
validate_file "$file"
else
echo "File $file has been deleted or is not accessible."
fi
done
# Exit with error if any validation failed
if [ "$error_occurred" = true ]; then
exit 1
fi
shell: bash