Test the script

This commit is contained in:
dvirlabs 2025-06-22 15:42:59 +03:00
parent 4c2f682fe5
commit 963eeef5d6

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
set -e set -e
# 📦 Install required tools (run via Woodpecker .woodpecker.yml) # 📦 Install required tools (for debugging / local run)
apk add --no-cache git bash curl yq jq apk add --no-cache git bash curl yq jq
echo "🔍 Scanning for apps with cname.yaml..." echo "🔍 Scanning for apps with cname.yaml..."
@ -20,7 +20,7 @@ ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml"
MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml"
# === STEP 1: Clone Repos === # === STEP 1: Clone Repos ===
echo "📦 Cloning sandbox-apps..." echo "📦 Cloning sandbox..."
git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE"
echo "📦 Cloning infra..." echo "📦 Cloning infra..."
@ -44,7 +44,7 @@ find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do
echo "✅ Found $hostname$service" echo "✅ Found $hostname$service"
# Append new entry to generated ingress list # Append new entry
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE"
fi fi
done done
@ -52,49 +52,28 @@ done
echo "📄 Generated Ingress:" echo "📄 Generated Ingress:"
cat "$GENERATED_FILE" cat "$GENERATED_FILE"
# === STEP 3: Merge with existing cloudflared values === # === STEP 3: Merge only ingress into existing cloudflared values.yaml ===
echo "🔁 Merging new entries into: $ORIGINAL_FILE" echo "🔁 Merging new entries into: $ORIGINAL_FILE"
TEMP_FILE=$(mktemp) yq eval-all '
# File 0 = Original cloudflared values
# Extract original ingress # File 1 = Generated ingress block
yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE" (select(fileIndex == 0) as $orig | select(fileIndex == 1) as $new |
$orig * {
# Append new unique entries cloudflare: $orig.cloudflare * {
yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do ingress: (
hostname=$(echo "$new_entry" | jq -r '.hostname') ($orig.cloudflare.ingress + $new.ingress)
service=$(echo "$new_entry" | jq -r '.service') | unique_by(.hostname)
)
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE") }
}
if [ -z "$exists" ]; then )
echo " Adding $hostname$service" ' "$ORIGINAL_FILE" "$GENERATED_FILE" > "$MERGED_FILE"
yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE"
else
echo "⚠️ $hostname already exists, skipping"
fi
done
# === 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:" echo "✅ Final merged values.yaml:"
cat "$MERGED_FILE" cat "$MERGED_FILE"
# === STEP 6: Optional Git push === # === STEP 4: Commit & Push ===
echo "📤 Pushing updated values.yaml to infra repo..." echo "📤 Pushing updated values.yaml to infra repo..."
cd "$INFRA_CLONE" cd "$INFRA_CLONE"