Update bind-everything.yml

This commit is contained in:
2025-09-28 09:23:03 -07:00
committed by GitHub
parent 33f2d0e56f
commit fe47e2879a

View File

@@ -1,5 +1,8 @@
name: Sync CF D1 & KV Bindings
on: on:
push: push:
branches: ["**"]
workflow_dispatch: workflow_dispatch:
permissions: permissions:
@@ -9,43 +12,50 @@ jobs:
sync: sync:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout Repository - uses: actions/checkout@v4
uses: actions/checkout@v4 - uses: actions/setup-node@v4
- name: Setup Node
uses: actions/setup-node@v4
with: with:
node-version: 20 node-version: 20
- name: Install Dependencies - name: Install Wrangler & jq
run: npm i -g wrangler run: |
npm i -g wrangler
sudo apt-get update -y && sudo apt-get install -y jq
- name: Sync Cloudflare Bindings - name: Fetch Account ID & Sync wrangler.toml
env: env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: | run: |
set -euo pipefail set -u
hdr=(-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json") AID=$(curl -fsSL https://api.cloudflare.com/client/v4/accounts \
AID=$(curl -fsSL "${hdr[@]}" https://api.cloudflare.com/client/v4/accounts | jq -r '.result[0].id // empty') -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \
[ -n "$AID" ] || { echo "::error::Failed to retrieve Cloudflare Account ID."; exit 1; } | jq -r '.result[0].id')
[ -n "$AID" ] || { echo "No account id"; exit 1; }
export CLOUDFLARE_ACCOUNT_ID="$AID"
touch wrangler.toml touch wrangler.toml
curl -fsSL "${hdr[@]}" "https://api.cloudflare.com/client/v4/accounts/$AID/d1/database/list?per_page=1000" \ # D1: append missing bindings
| jq -Mc '.result[]' \ (wrangler d1 list --json 2>/dev/null || echo '[]') \
| while read -r db; do | jq -c '.[]?' | while read -r db; do
id=$(jq -r .uuid<<<"$db"); name=$(jq -r .name<<<"$db"); bind=$(tr '[:lower:]-' '[:upper:]_'<<<"$name") id=$(jq -r '.uuid' <<<"$db"); grep -q "$id" wrangler.toml && continue
grep -q "$id" wrangler.toml || printf '\n[[d1_databases]]\nbinding="%s"\ndatabase_name="%s"\ndatabase_id="%s"\n' "$bind" "$name" "$id" >> wrangler.toml 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 done
curl -fsSL "${hdr[@]}" "https://api.cloudflare.com/client/v4/accounts/$AID/storage/kv/namespaces?per_page=1000" \ # KV: append missing namespaces
| jq -Mc '.result[]' \ curl -fsSL "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/storage/kv/namespaces?per_page=1000" \
| while read -r kv; do -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \
id=$(jq -r .id<<<"$kv"); title=$(jq -r .title<<<"$kv"); bind=$(tr '[:lower:]-' '[:upper:]_'<<<"$title") | jq -c '.result[]?' | while read -r ns; do
grep -q "$id" wrangler.toml || printf '\n[[kv_namespaces]]\nbinding="%s"\nid="%s"\n' "$bind" "$id" >> wrangler.toml 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 done
- name: Commit and Push Changes - name: Commit and Push Changes
uses: stefanzweifel/git-auto-commit-action@v5 uses: stefanzweifel/git-auto-commit-action@v5
with: with:
commit_message: "chore: Sync Cloudflare D1 & KV bindings" commit_message: "chore: sync D1 + KV bindings"
file_pattern: wrangler.toml file_pattern: wrangler.toml