From 499e31ebe8d4796f45a3d7c0dfc9fb4b114a08c3 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Sun, 28 Sep 2025 09:28:50 -0700 Subject: [PATCH] Update bind-everything.yml --- .github/workflows/bind-everything.yml | 57 ++++++++++++++++++--------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/.github/workflows/bind-everything.yml b/.github/workflows/bind-everything.yml index 63038a1..733f125 100644 --- a/.github/workflows/bind-everything.yml +++ b/.github/workflows/bind-everything.yml @@ -1,5 +1,8 @@ +name: Sync CF D1 & KV Bindings + on: push: + branches: ["**"] workflow_dispatch: permissions: @@ -9,34 +12,50 @@ jobs: sync: runs-on: ubuntu-latest steps: - - name: Checkout Repository - uses: actions/checkout@v4 - - - name: Setup Node - uses: actions/setup-node@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 20 - - name: Install Dependencies - run: npm install -g wrangler + - name: Install Wrangler & jq + run: | + npm i -g wrangler + sudo apt-get update -y && sudo apt-get install -y jq - - name: Fetch Cloudflare Account ID & Sync Bindings + - name: Fetch Account ID & Sync wrangler.toml env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} run: | - export CLOUDFLARE_ACCOUNT_ID=$(curl -sX GET "https://api.cloudflare.com/client/v4/accounts" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type:application/json" | jq -r '.result[0].id') - wrangler d1 list --json | jq -c '.[]' | while read -r db; do - id=$(jq -r .uuid <<< "$db") - if ! grep -q "$id" wrangler.toml; then - name=$(jq -r .name <<< "$db") - binding=$(echo "$name" | tr '[:lower:]-' '[:upper:]_') - printf '\n[[d1_databases]]\nbinding = "%s"\ndatabase_name = "%s"\ndatabase_id = "%s"\n' "$binding" "$name" "$id" >> wrangler.toml - fi - done + set -u + AID=$(curl -fsSL https://api.cloudflare.com/client/v4/accounts \ + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \ + | jq -r '.result[0].id') + [ -n "$AID" ] || { echo "No account id"; exit 1; } + export CLOUDFLARE_ACCOUNT_ID="$AID" + + touch wrangler.toml + + # D1: append missing bindings + (wrangler d1 list --json 2>/dev/null || echo '[]') \ + | jq -c '.[]?' | while read -r db; do + id=$(jq -r '.uuid' <<<"$db"); grep -q "$id" wrangler.toml && continue + name=$(jq -r '.name' <<<"$db") + bind=$(printf "%s" "$name" | tr '[:lower:]' '[:upper:]' | sed -E 's/[^A-Z0-9]+/_/g') + printf '\n[[d1_databases]]\nbinding="%s"\ndatabase_name="%s"\ndatabase_id="%s"\n' "$bind" "$name" "$id" >> wrangler.toml + done + + # KV: append missing namespaces + curl -fsSL "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces?per_page=1000" \ + -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \ + | jq -c '.result[]?' | while read -r ns; do + id=$(jq -r '.id' <<<"$ns"); grep -q "$id" wrangler.toml && continue + title=$(jq -r '.title' <<<"$ns") + bind=$(printf "%s" "$title" | tr '[:lower:]' '[:upper:]' | sed -E 's/[^A-Z0-9]+/_/g') + printf '\n[[kv_namespaces]]\nbinding="%s"\nid="%s"\n' "$bind" "$id" >> wrangler.toml + done - name: Commit and Push Changes uses: stefanzweifel/git-auto-commit-action@v5 with: - commit_message: "chore: Sync D1 database bindings" + commit_message: "chore: sync D1 + KV bindings" file_pattern: wrangler.toml -