diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index 2b07d39..e608f14 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -2,6 +2,8 @@ set -e # đŸ“Ļ Ensure apk installs: yq, jq, git, bash, curl +# Required for pipeline: alpine image + 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..." @@ -26,9 +28,11 @@ git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" echo "đŸ“Ļ Cloning infra..." git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" -# === STEP 2: Extract CNAMEs from sandbox === -echo "âš™ī¸ Generating ingress list..." -echo "ingress: []" > "$GENERATED_FILE" +# === STEP 2: Extract Generated CNAMEs from sandbox === +echo "âš™ī¸ Generating sandbox ingress list..." +cat < "$GENERATED_FILE" +ingress: [] +EOF find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do app_name=$(basename "$(dirname "$cname_file")") @@ -36,58 +40,46 @@ find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do if [[ "$enabled" == "true" ]]; then hostname=$(yq '.hostname' "$cname_file") - section=$(yq '.section // "unspecified"' "$cname_file") service="http://${app_name}.sandbox.svc.cluster.local:80" - echo "✅ Found $hostname ($section) → $service" - yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"section\": \"$section\"}]" -i "$GENERATED_FILE" + echo "✅ Found $hostname → $service" + + yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" fi done +echo "📄 Generated Ingress:" cat "$GENERATED_FILE" # === STEP 3: Merge with existing cloudflared values === -echo "🔁 Merging with: $ORIGINAL_FILE" +echo "🔁 Merging new entries into: $ORIGINAL_FILE" + TEMP_FILE=$(mktemp) cp "$ORIGINAL_FILE" "$TEMP_FILE" -# Loop through new entries and append if hostname not exists +# Loop through new entries and append only if hostname not exists yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do hostname=$(echo "$new_entry" | jq -r '.hostname') service=$(echo "$new_entry" | jq -r '.service') - section=$(echo "$new_entry" | jq -r '.section') exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$TEMP_FILE") if [ -z "$exists" ]; then - echo "➕ Adding $hostname ($section)" - yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"section\": \"$section\"}]" -i "$TEMP_FILE" + echo "➕ Adding $hostname → $service" + yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE" else echo "âš ī¸ $hostname already exists, skipping" fi done -# === STEP 4: Group & Write Final values.yaml === -echo "📝 Grouping by section and writing $MERGED_FILE" -echo "cloudflare:" > "$MERGED_FILE" -yq eval '.cloudflare | del(.ingress)' "$TEMP_FILE" | tail -n +2 >> "$MERGED_FILE" -echo " ingress:" >> "$MERGED_FILE" - -yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.section)[]' | while read -r group; do - section=$(echo "$group" | jq -r '.[0].section') - echo " # ##### $section #####" >> "$MERGED_FILE" - echo "$group" | jq -c '.[]' | while read -r item; do - hostname=$(echo "$item" | jq -r '.hostname') - service=$(echo "$item" | jq -r '.service') - echo " - hostname: $hostname" >> "$MERGED_FILE" - echo " service: $service" >> "$MERGED_FILE" - done - echo >> "$MERGED_FILE" -done +# === STEP 4: Overwrite only ingress list and preserve all other fields === +echo "📝 Writing final merged values.yaml" +cp "$TEMP_FILE" "$MERGED_FILE" +echo "✅ Final merged values.yaml:" cat "$MERGED_FILE" -# === STEP 5: Git Commit & Push === +# === STEP 5: Optional push to Git === cd "$INFRA_CLONE" git config user.name "woodpecker-bot" git config user.email "ci@dvirlabs.com" @@ -95,24 +87,27 @@ git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra. if ! git diff --quiet manifests/cloudflared/values.yaml; then git add manifests/cloudflared/values.yaml - git commit -m "chore(cloudflared): auto-grouped ingress entries by section" + git commit -m "chore(cloudflared): auto-merge CNAME entries from sandbox" git push origin HEAD echo "✅ Changes pushed successfully." else echo "â„šī¸ No changes to commit." fi -# === STEP 6: Create CNAMEs in Cloudflare === +# === STEP 5: Create CNAME records in Cloudflare === echo "🌐 Creating CNAME records in Cloudflare..." CLOUDFLARE_API="https://api.cloudflare.com/client/v4" TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com" +# Load required secrets : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}" : "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}" +# Check and create each CNAME yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r record; do name=$(echo "$record" | jq -r '.hostname' | sed 's/\.dvirlabs\.com//') + # Check if already exists exists=$(curl -s -X GET "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$name.dvirlabs.com" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | jq '.result | length') @@ -121,7 +116,14 @@ yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | whi curl -s -X POST "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Content-Type: application/json" \ - --data "{\n \"type\": \"CNAME\",\n \"name\": \"$name\",\n \"content\": \"$TARGET\",\n \"ttl\": 1,\n \"proxied\": true\n }" > /dev/null + --data "{ + \"type\": \"CNAME\", + \"name\": \"$name\", + \"content\": \"$TARGET\", + \"ttl\": 1, + \"proxied\": true + }" > /dev/null else echo "âš ī¸ CNAME for $name.dvirlabs.com already exists, skipping" fi +done \ No newline at end of file