From 40323a744bc439457ef00f97b2de1b5dbeb41601 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Sat, 21 Jun 2025 22:18:35 +0300 Subject: [PATCH] Fix script to append and not override --- automation/cloudflared-sync.sh | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index fbc3e1a..b5f4ce5 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -2,8 +2,6 @@ set -e # đŸ“Ļ Install required tools (run via Woodpecker .woodpecker.yml) -# Ensure apk installs: yq, jq, git, bash, curl - apk add --no-cache git bash curl yq jq echo "🔍 Scanning for apps with cname.yaml..." @@ -59,10 +57,10 @@ echo "🔁 Merging new entries into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) -# Copy original ingress list +# Extract original ingress yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE" -# Append new unique entries (skip existing hostnames) +# Append new unique entries yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do hostname=$(echo "$new_entry" | jq -r '.hostname') service=$(echo "$new_entry" | jq -r '.service') @@ -77,19 +75,26 @@ yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | whi fi done -# === STEP 4: Write merged file === -yq eval ' - .cloudflare.ingress = load("'"$TEMP_FILE"'") | - .cloudflare.tunnelName = .cloudflare.tunnelName | - .cloudflare.enableWarp = .cloudflare.enableWarp | - .cloudflare.secretName = .cloudflare.secretName | - .cloudflared = .cloudflared -' "$ORIGINAL_FILE" > "$MERGED_FILE" +# === STEP 4: Load preserved values === +TUNNEL_NAME=$(yq e '.cloudflare.tunnelName' "$ORIGINAL_FILE") +ENABLE_WARP=$(yq e '.cloudflare.enableWarp' "$ORIGINAL_FILE") +SECRET_NAME=$(yq e '.cloudflare.secretName' "$ORIGINAL_FILE") +CLOUDFLARED_BLOCK=$(yq e '.cloudflared' "$ORIGINAL_FILE") + +# === STEP 5: Write final merged values === +echo "📝 Writing merged file to $MERGED_FILE" +yq eval " +.cloudflare.ingress = load(\"$TEMP_FILE\") | +.cloudflare.tunnelName = \"$TUNNEL_NAME\" | +.cloudflare.enableWarp = \"$ENABLE_WARP\" | +.cloudflare.secretName = \"$SECRET_NAME\" | +.cloudflared = $CLOUDFLARED_BLOCK +" "$ORIGINAL_FILE" > "$MERGED_FILE" echo "✅ Final merged values.yaml:" cat "$MERGED_FILE" -# === STEP 5: Optional push to Git === +# === STEP 6: Optional Git push === echo "📤 Pushing updated values.yaml to infra repo..." cd "$INFRA_CLONE" @@ -97,7 +102,6 @@ git config user.name "woodpecker-bot" git config user.email "ci@dvirlabs.com" git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git" -# Only commit if there are changes if ! git diff --quiet manifests/cloudflared/values.yaml; then git add manifests/cloudflared/values.yaml git commit -m "chore(cloudflared): auto-merge CNAME entries from sandbox" @@ -106,4 +110,3 @@ if ! git diff --quiet manifests/cloudflared/values.yaml; then else echo "â„šī¸ No changes to commit." fi -