on: push: workflow_dispatch: permissions: contents: write jobs: sync: runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v4 - name: Setup Node uses: actions/setup-node@v4 with: node-version: 20 - name: Install Dependencies run: npm i -g wrangler - name: Sync Cloudflare Bindings env: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} run: | set -euo pipefail hdr=(-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json") AID=$(curl -fsSL "${hdr[@]}" https://api.cloudflare.com/client/v4/accounts | jq -r '.result[0].id // empty') [ -n "$AID" ] || { echo "::error::Failed to retrieve Cloudflare Account ID."; exit 1; } touch wrangler.toml curl -fsSL "${hdr[@]}" "https://api.cloudflare.com/client/v4/accounts/$AID/d1/database/list?per_page=1000" \ | jq -Mc '.result[]' \ | while read -r db; do id=$(jq -r .uuid<<<"$db"); name=$(jq -r .name<<<"$db"); bind=$(tr '[:lower:]-' '[:upper:]_'<<<"$name") grep -q "$id" wrangler.toml || printf '\n[[d1_databases]]\nbinding="%s"\ndatabase_name="%s"\ndatabase_id="%s"\n' "$bind" "$name" "$id" >> wrangler.toml done curl -fsSL "${hdr[@]}" "https://api.cloudflare.com/client/v4/accounts/$AID/storage/kv/namespaces?per_page=1000" \ | jq -Mc '.result[]' \ | while read -r kv; do id=$(jq -r .id<<<"$kv"); title=$(jq -r .title<<<"$kv"); bind=$(tr '[:lower:]-' '[:upper:]_'<<<"$title") grep -q "$id" wrangler.toml || 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 Cloudflare D1 & KV bindings" file_pattern: wrangler.toml