diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index 21e861e..4871fae 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -50,7 +50,6 @@ echo "๐Ÿ” Merging new entries into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) cp "$ORIGINAL_FILE" "$TEMP_FILE" -# Strip namespace key from existing values if present yq eval -o=json '.cloudflare.ingress' "$TEMP_FILE" > /tmp/existing.json yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do @@ -68,14 +67,12 @@ yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r fi done -# === ๐Ÿงน Group and sort with comments === echo "๐Ÿงผ Grouping ingress entries by namespace..." GROUPED=$(mktemp) - yq eval 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$GROUPED" + echo " ingress:" >> "$GROUPED" -# Extract ingress entries with namespace yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.namespace)[]' | while read -r group; do namespace=$(echo "$group" | jq -r '.[0].namespace') echo " # ############ $namespace ############" >> "$GROUPED" @@ -85,15 +82,15 @@ yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.names echo " - hostname: $hostname" >> "$GROUPED" echo " service: $service" >> "$GROUPED" done -done +done >> "$GROUPED" + +sed -i '/^cloudflare:/r /dev/stdin' "$GROUPED" <<< "$(tail -n +2 "$GROUPED")" -# Copy to merged location cp "$GROUPED" "$MERGED_FILE" echo "โœ… Final merged values.yaml:" cat "$MERGED_FILE" -# === Commit if changed === cd "$INFRA_CLONE" git config user.name "woodpecker-bot" git config user.email "ci@dvirlabs.com" @@ -108,7 +105,6 @@ else echo "โ„น๏ธ No changes to commit." fi -# === Create Cloudflare CNAMEs === echo "๐ŸŒ Creating CNAME records in Cloudflare..." CLOUDFLARE_API="https://api.cloudflare.com/client/v4" TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com" @@ -116,7 +112,7 @@ TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}" : "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}" -yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' | jq -c '.[]' | while read -r record; do +yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r record; do name=$(echo "$record" | jq -r '.hostname' | sed 's/\.dvirlabs\.com//') exists=$(curl -s -X GET "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$name.dvirlabs.com" \