Skip to content

Commit 9330d37

Browse files
committed
new file: .github/scripts/inject_config.py
modified: .github/workflows/deploy.yml
1 parent ab2f030 commit 9330d37

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

.github/scripts/inject_config.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
"""
3+
inject_config.py — run by GitHub Actions during deployment.
4+
5+
Reads CV_API_KEY, CV_PROVIDER, CV_MODEL from environment variables
6+
(populated from GitHub Secrets/Variables) and writes a safe js/config.js.
7+
8+
Uses json.dumps() so special characters in the key ($, ", \, etc.)
9+
are always correctly escaped as JavaScript string literals.
10+
"""
11+
import os
12+
import json
13+
14+
api_key = os.environ.get("CV_API_KEY", "")
15+
provider = os.environ.get("CV_PROVIDER", "") or "claude"
16+
model = os.environ.get("CV_MODEL", "") or "claude-sonnet-4-5-20250929"
17+
18+
lines = [
19+
"// Auto-generated by GitHub Actions -- do not edit manually.",
20+
"// Regenerated on every push to main.",
21+
"// To update: change the CLEARVOICE_API_KEY secret in repository settings.",
22+
"window.CV_CONFIG = {",
23+
" apiKey : " + json.dumps(api_key) + ",",
24+
" provider: " + json.dumps(provider) + ",",
25+
" model : " + json.dumps(model) + ",",
26+
"};",
27+
"",
28+
]
29+
30+
with open("js/config.js", "w") as f:
31+
f.write("\n".join(lines))
32+
33+
if api_key:
34+
print("API key injected into js/config.js")
35+
else:
36+
print("No CLEARVOICE_API_KEY secret found -- deploying without key (users configure manually).")

.github/workflows/deploy.yml

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,50 @@
11
# =============================================================
2-
# ClearVoice GitHub Actions Deployment Workflow
2+
# ClearVoice - GitHub Actions Deployment Workflow
33
#
4-
# This workflow injects your API key from GitHub Secrets at
5-
# build time, then deploys the site to GitHub Pages.
6-
#
7-
# HOW TO SET UP:
8-
# 1. Go to your repo → Settings → Secrets and variables → Actions
4+
# HOW TO SET UP GITHUB SECRETS:
5+
# 1. Go to your repo -> Settings -> Secrets and variables -> Actions
96
# 2. Click "New repository secret"
107
# 3. Name: CLEARVOICE_API_KEY | Value: your Anthropic API key
11-
# 4. Optionally add: CLEARVOICE_MODEL (default: claude-sonnet-4-5-20250929)
12-
# 5. Push to main or trigger manually via "Run workflow"
8+
# 4. Optionally add Variables (not Secrets) for provider/model:
9+
# CLEARVOICE_PROVIDER (default: claude)
10+
# CLEARVOICE_MODEL (default: claude-sonnet-4-5-20250929)
11+
# 5. Push to main - the workflow runs automatically.
1312
#
1413
# SECURITY NOTE:
15-
# The injected key will exist in the deployed GitHub Pages files.
16-
# This is acceptable for personal/private projects, but be aware
17-
# the key could be read by anyone who inspects your site's JS source.
18-
# For team use, consider a backend proxy instead.
14+
# The injected key exists in the deployed GitHub Pages JS files.
15+
# Suitable for personal projects. For team use, use a backend proxy.
1916
# =============================================================
2017

2118
name: Deploy ClearVoice to GitHub Pages
2219

2320
on:
2421
push:
2522
branches: [main]
26-
workflow_dispatch: # Allow manual trigger from the Actions tab
23+
workflow_dispatch:
2724

2825
permissions:
2926
contents: read
3027
pages: write
3128
id-token: write
3229

33-
# Only one deployment runs at a time
3430
concurrency:
35-
group: "pages"
31+
group: pages
3632
cancel-in-progress: false
3733

3834
jobs:
39-
# ── Build ────────────────────────────────────────────────
35+
4036
build:
4137
runs-on: ubuntu-latest
4238
steps:
4339
- name: Checkout repository
4440
uses: actions/checkout@v4
4541

46-
- name: Generate js/config.js with injected secrets
42+
- name: Inject API key into js/config.js
4743
env:
48-
CV_API_KEY : ${{ secrets.CLEARVOICE_API_KEY }}
44+
CV_API_KEY: ${{ secrets.CLEARVOICE_API_KEY }}
4945
CV_PROVIDER: ${{ vars.CLEARVOICE_PROVIDER }}
50-
CV_MODEL : ${{ vars.CLEARVOICE_MODEL }}
51-
# Python is used for safe JSON string encoding of the API key
52-
# (handles special characters like $, ", \, etc.)
53-
run: |
54-
python3 - <<'PYEOF'
55-
import os, json
56-
57-
api_key = os.environ.get("CV_API_KEY", "")
58-
provider = os.environ.get("CV_PROVIDER", "claude") or "claude"
59-
model = os.environ.get("CV_MODEL", "claude-sonnet-4-5-20250929") or "claude-sonnet-4-5-20250929"
60-
61-
content = f"""// Auto-generated by GitHub Actions — DO NOT EDIT
62-
// Regenerated on every push to main.
63-
// To update: change the CLEARVOICE_API_KEY secret in repository settings.
64-
window.CV_CONFIG = {{
65-
apiKey : {json.dumps(api_key)},
66-
provider: {json.dumps(provider)},
67-
model : {json.dumps(model)},
68-
}};
69-
"""
70-
with open("js/config.js", "w") as f:
71-
f.write(content)
72-
73-
if api_key:
74-
print("✅ API key injected into js/config.js")
75-
else:
76-
print("⚠️ No CLEARVOICE_API_KEY secret found — deploying without key (users will configure manually)")
77-
PYEOF
46+
CV_MODEL: ${{ vars.CLEARVOICE_MODEL }}
47+
run: python3 .github/scripts/inject_config.py
7848

7949
- name: Setup GitHub Pages
8050
uses: actions/configure-pages@v4
@@ -84,7 +54,6 @@ window.CV_CONFIG = {{
8454
with:
8555
path: "."
8656

87-
# ── Deploy ───────────────────────────────────────────────
8857
deploy:
8958
environment:
9059
name: github-pages

0 commit comments

Comments
 (0)