feat: update workflows and README for KV and R2 mode switching

This commit is contained in:
shuaiplus
2026-03-07 02:33:29 +08:00
parent 1aa29dda11
commit 23c78b3408
6 changed files with 242 additions and 61 deletions
+80
View File
@@ -0,0 +1,80 @@
name: Switch to KV mode
on:
workflow_dispatch:
permissions:
contents: write
jobs:
patch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Switch wrangler.toml to KV mode
run: |
python - <<'PY'
import re
from pathlib import Path
path = Path("wrangler.toml")
text = path.read_text(encoding="utf-8")
pattern = (
r"\[\[r2_buckets\]\]\s*"
r'binding\s*=\s*"ATTACHMENTS"\s*'
r'bucket_name\s*=\s*"nodewarden-attachments"\s*'
)
replacement = (
'[[kv_namespaces]]\n'
'binding = "ATTACHMENTS_KV"\n'
'id = "placeholder"\n'
)
new_text, count = re.subn(pattern, replacement, text, count=1)
if count == 0 and 'binding = "ATTACHMENTS_KV"' not in text:
raise SystemExit("Expected R2 block not found in wrangler.toml")
if count > 0:
path.write_text(new_text, encoding="utf-8")
PY
- name: Update deploy button link in README files
env:
REPO_SLUG: ${{ github.repository }}
run: |
python - <<'PY'
import os
import re
from pathlib import Path
repo_slug = os.environ["REPO_SLUG"]
deploy_url = f"https://deploy.workers.cloudflare.com/?url=https://github.com/{repo_slug}"
pattern = re.compile(
r'https://deploy\.workers\.cloudflare\.com/\?url=https://github\.com/[^)\s]+'
)
for file_name in ("README.md", "README_EN.md"):
path = Path(file_name)
text = path.read_text(encoding="utf-8")
new_text, count = pattern.subn(deploy_url, text, count=2)
if count > 0:
path.write_text(new_text, encoding="utf-8")
PY
- name: Commit KV mode change
run: |
git add wrangler.toml README.md README_EN.md
git diff --cached --quiet && exit 0
git commit -m "chore: switch wrangler.toml to KV mode"
git push origin main
+80
View File
@@ -0,0 +1,80 @@
name: Switch to R2 mode
on:
workflow_dispatch:
permissions:
contents: write
jobs:
patch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Switch wrangler.toml to R2 mode
run: |
python - <<'PY'
import re
from pathlib import Path
path = Path("wrangler.toml")
text = path.read_text(encoding="utf-8")
pattern = (
r"\[\[kv_namespaces\]\]\s*"
r'binding\s*=\s*"ATTACHMENTS_KV"\s*'
r'id\s*=\s*"placeholder"\s*'
)
replacement = (
'[[r2_buckets]]\n'
'binding = "ATTACHMENTS"\n'
'bucket_name = "nodewarden-attachments"\n'
)
new_text, count = re.subn(pattern, replacement, text, count=1)
if count == 0 and 'binding = "ATTACHMENTS"' not in text:
raise SystemExit("Expected KV block not found in wrangler.toml")
if count > 0:
path.write_text(new_text, encoding="utf-8")
PY
- name: Update deploy button link in README files
env:
REPO_SLUG: ${{ github.repository }}
run: |
python - <<'PY'
import os
import re
from pathlib import Path
repo_slug = os.environ["REPO_SLUG"]
deploy_url = f"https://deploy.workers.cloudflare.com/?url=https://github.com/{repo_slug}"
pattern = re.compile(
r'https://deploy\.workers\.cloudflare\.com/\?url=https://github\.com/[^)\s]+'
)
for file_name in ("README.md", "README_EN.md"):
path = Path(file_name)
text = path.read_text(encoding="utf-8")
new_text, count = pattern.subn(deploy_url, text, count=2)
if count > 0:
path.write_text(new_text, encoding="utf-8")
PY
- name: Commit R2 mode change
run: |
git add wrangler.toml README.md README_EN.md
git diff --cached --quiet && exit 0
git commit -m "chore: switch wrangler.toml to R2 mode"
git push origin main
+22 -30
View File
@@ -4,9 +4,6 @@ on:
schedule:
- cron: "0 3 * * *"
workflow_dispatch:
push:
branches:
- main
permissions:
contents: write
@@ -25,45 +22,40 @@ jobs:
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Sync main from upstream
- name: Sync main from upstream but keep local wrangler.toml
run: |
cp wrangler.toml /tmp/nodewarden-wrangler.toml
git remote add upstream https://github.com/shuaiplus/NodeWarden.git || true
git fetch upstream
git checkout main
git reset --hard upstream/main
git push origin main --force
cp /tmp/nodewarden-wrangler.toml wrangler.toml
- name: Rebuild kv from main
- name: Update deploy button links in README files
env:
REPO_SLUG: ${{ github.repository }}
run: |
git fetch origin main kv || true
git checkout -B kv origin/main
python - <<'PY'
import os
import re
from pathlib import Path
path = Path("wrangler.toml")
text = path.read_text(encoding="utf-8")
pattern = (
r"\[\[r2_buckets\]\]\s*"
r'binding\s*=\s*"ATTACHMENTS"\s*'
r'bucket_name\s*=\s*"nodewarden-attachments"\s*'
)
replacement = (
'[[kv_namespaces]]\n'
'binding = "ATTACHMENTS_KV"\n'
'id = "REPLACE_WITH_KV_NAMESPACE_ID"\n'
repo_slug = os.environ["REPO_SLUG"]
deploy_url = f"https://deploy.workers.cloudflare.com/?url=https://github.com/{repo_slug}"
pattern = re.compile(
r'https://deploy\.workers\.cloudflare\.com/\?url=https://github\.com/[^)\s]+'
)
new_text, count = re.subn(pattern, replacement, text, count=1)
if count == 0:
raise SystemExit("Expected R2 block not found in wrangler.toml")
path.write_text(new_text, encoding="utf-8")
for file_name in ("README.md", "README_EN.md"):
path = Path(file_name)
text = path.read_text(encoding="utf-8")
new_text, _ = pattern.subn(deploy_url, text)
path.write_text(new_text, encoding="utf-8")
PY
git add wrangler.toml
git commit -m "chore(kv): sync from main" || echo "No changes"
git push origin kv --force
- name: Push synced main
run: |
git add wrangler.toml README.md README_EN.md
git diff --cached --quiet && exit 0
git commit -m "chore: sync upstream while keeping local deploy config"
git push origin main --force