Test the script
This commit is contained in:
parent
4dabfc4e28
commit
7d8f5167c8
@ -1,8 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# 📦 Install required tools (run via Woodpecker .woodpecker.yml)
|
# 📦 Ensure apk installs: yq, jq, git, bash, curl
|
||||||
# Ensure apk installs: yq, jq, git, bash, curl
|
# Required for pipeline: alpine image + apk add --no-cache git bash curl yq jq
|
||||||
|
|
||||||
echo "🔍 Scanning for apps with cname.yaml..."
|
echo "🔍 Scanning for apps with cname.yaml..."
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ mkdir -p .tmp-repos
|
|||||||
|
|
||||||
# === REPO CONFIG ===
|
# === REPO CONFIG ===
|
||||||
SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.git"
|
SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.git"
|
||||||
INFRA_REPO_URL="https://git.dvirlabs.com/dvirlabs/infra.git"
|
INFRA_REPO_URL="https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git"
|
||||||
SANDBOX_CLONE=".tmp-repos/sandbox"
|
SANDBOX_CLONE=".tmp-repos/sandbox"
|
||||||
INFRA_CLONE=".tmp-repos/infra"
|
INFRA_CLONE=".tmp-repos/infra"
|
||||||
GENERATED_FILE="generated-values/cloudflared-values.yaml"
|
GENERATED_FILE="generated-values/cloudflared-values.yaml"
|
||||||
@ -44,7 +44,6 @@ 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
|
|
||||||
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE"
|
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -56,41 +55,41 @@ cat "$GENERATED_FILE"
|
|||||||
echo "🔁 Merging new entries into: $ORIGINAL_FILE"
|
echo "🔁 Merging new entries into: $ORIGINAL_FILE"
|
||||||
|
|
||||||
TEMP_FILE=$(mktemp)
|
TEMP_FILE=$(mktemp)
|
||||||
|
cp "$ORIGINAL_FILE" "$TEMP_FILE"
|
||||||
|
|
||||||
# Copy original ingress list
|
# Loop through new entries and append only if hostname not exists
|
||||||
yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE"
|
yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do
|
||||||
|
|
||||||
# Append new unique entries (skip existing hostnames)
|
|
||||||
yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do
|
|
||||||
hostname=$(echo "$new_entry" | jq -r '.hostname')
|
hostname=$(echo "$new_entry" | jq -r '.hostname')
|
||||||
service=$(echo "$new_entry" | jq -r '.service')
|
service=$(echo "$new_entry" | jq -r '.service')
|
||||||
|
|
||||||
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE")
|
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$TEMP_FILE")
|
||||||
|
|
||||||
if [ -z "$exists" ]; then
|
if [ -z "$exists" ]; then
|
||||||
echo "➕ Adding $hostname → $service"
|
echo "➕ Adding $hostname → $service"
|
||||||
yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE"
|
yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE"
|
||||||
else
|
else
|
||||||
echo "⚠️ $hostname already exists, skipping"
|
echo "⚠️ $hostname already exists, skipping"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# === STEP 4: Write merged file ===
|
# === STEP 4: Overwrite only ingress list and preserve all other fields ===
|
||||||
yq eval '
|
echo "📝 Writing final merged values.yaml"
|
||||||
.cloudflare.ingress = load("'"$TEMP_FILE"'") |
|
cp "$TEMP_FILE" "$MERGED_FILE"
|
||||||
.cloudflare.tunnelName = .cloudflare.tunnelName |
|
|
||||||
.cloudflare.enableWarp = .cloudflare.enableWarp |
|
|
||||||
.cloudflare.secretName = .cloudflare.secretName |
|
|
||||||
.cloudflared = .cloudflared
|
|
||||||
' "$ORIGINAL_FILE" > "$MERGED_FILE"
|
|
||||||
|
|
||||||
echo "✅ Final merged values.yaml:"
|
echo "✅ Final merged values.yaml:"
|
||||||
cat "$MERGED_FILE"
|
cat "$MERGED_FILE"
|
||||||
|
|
||||||
# === STEP 5: Optional push to Git ===
|
# === STEP 5: Optional push to Git ===
|
||||||
# cd "$INFRA_CLONE"
|
cd "$INFRA_CLONE"
|
||||||
# git config user.name "woodpecker-bot"
|
git config user.name "woodpecker-bot"
|
||||||
# git config user.email "ci@dvirlabs.com"
|
git config user.email "ci@dvirlabs.com"
|
||||||
# git add manifests/cloudflared/values.yaml
|
git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git"
|
||||||
# git commit -m "chore(cloudflared): auto-add ingress from sandbox"
|
|
||||||
# git push
|
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"
|
||||||
|
git push origin HEAD
|
||||||
|
echo "✅ Changes pushed successfully."
|
||||||
|
else
|
||||||
|
echo "ℹ️ No changes to commit."
|
||||||
|
fi
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user