Update bind-everything.yml

This commit is contained in:
2025-09-28 09:28:50 -07:00
committed by GitHub
parent 9e5bf7f0c9
commit 499e31ebe8

View File

@@ -1,5 +1,8 @@
name: Sync CF D1 & KV Bindings
on: on:
push: push:
branches: ["**"]
workflow_dispatch: workflow_dispatch:
permissions: permissions:
@@ -9,34 +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 install -g wrangler 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: env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
run: | 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') set -u
wrangler d1 list --json | jq -c '.[]' | while read -r db; do AID=$(curl -fsSL https://api.cloudflare.com/client/v4/accounts \
id=$(jq -r .uuid <<< "$db") -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" \
if ! grep -q "$id" wrangler.toml; then | jq -r '.result[0].id')
name=$(jq -r .name <<< "$db") [ -n "$AID" ] || { echo "No account id"; exit 1; }
binding=$(echo "$name" | tr '[:lower:]-' '[:upper:]_') export CLOUDFLARE_ACCOUNT_ID="$AID"
printf '\n[[d1_databases]]\nbinding = "%s"\ndatabase_name = "%s"\ndatabase_id = "%s"\n' "$binding" "$name" "$id" >> wrangler.toml
fi 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 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 D1 database bindings" commit_message: "chore: sync D1 + KV bindings"
file_pattern: wrangler.toml file_pattern: wrangler.toml