Create validate-namespace.yaml #1
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Validate config-control Namespace | |
on: | |
paths: | |
- 'dns-records/**/*.yaml' | |
- 'dns-records/**/*.yml' | |
jobs: | |
validate-namespace: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
- name: Validate namespace | |
id: validate-namespace | |
run: | | |
files_with_namespace=() | |
yaml_files=() | |
EXPECTED_NAMESPACE='config-control' | |
# Find yml/yaml files that have been changed | |
changed_files=$(git diff --name-only HEAD^..HEAD) | |
echo "Changed Files" | |
echo "$changed_files" | |
if [ -n "$changed_files" ]; then | |
while IFS= read -r file; do | |
if [[ "$file" =~ \.y(a)?ml$ ]]; then | |
yaml_files+=("$file") | |
fi | |
done < <(echo "$changed_files") | |
echo -e "\nFound yaml files:" | |
for file in "${yaml_files[@]}"; do | |
echo "$file" | |
done | |
else | |
echo -e "\nNo YAML files were changed." | |
exit 0 | |
fi | |
# Check if there are any YAML files to process | |
if [ ${#yaml_files[@]} -eq 0 ]; then | |
echo -e "\nNo YAML files to process after filtering. Exiting cleanly." | |
exit 0 | |
fi | |
# Find yaml files that have .metadata.namespace defined | |
for file in "${yaml_files[@]}"; do | |
if [ -f "$file" ]; then # Ensure the file still exists in the repository | |
NAMESPACE=$(yq e '.metadata.namespace // ""' "$file") | |
if [ "$NAMESPACE" != "$EXPECTED_NAMESPACE" ]; then | |
echo "::error file=$FILE::Namespace is '$NAMESPACE', expected '$EXPECTED_NAMESPACE'" | |
exit 1 | |
else | |
echo "Namespace validation passed" | |
fi | |
else | |
echo "File $file has been deleted or is not accessible." | |
fi | |
done | |
shell: bash | |
- name: Install yq | |
uses: mikefarah/yq@v4 |