|
| 1 | +name: Deploy Strategy Switch Console |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - ".github/workflows/deploy-strategy-switch-console.yml" |
| 8 | + - "scripts/sync_strategy_switch_page_asset.py" |
| 9 | + - "web/strategy-switch-console/**" |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + sync_strategy_profiles: |
| 13 | + description: "After deploy, write the bundled strategy profile catalog to KV." |
| 14 | + required: true |
| 15 | + type: boolean |
| 16 | + default: true |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: strategy-switch-console-deploy |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + deploy: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + environment: runtime-strategy-switch |
| 29 | + timeout-minutes: 15 |
| 30 | + env: |
| 31 | + WORKER_DIR: web/strategy-switch-console |
| 32 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 33 | + CLOUDFLARE_WRANGLER_CONFIG_TOML: ${{ secrets.CLOUDFLARE_WRANGLER_CONFIG_TOML }} |
| 34 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID || vars.CLOUDFLARE_ACCOUNT_ID }} |
| 35 | + STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID: ${{ secrets.STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID || vars.STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID }} |
| 36 | + STRATEGY_SWITCH_CONSOLE_URL: ${{ vars.STRATEGY_SWITCH_CONSOLE_URL }} |
| 37 | + STRATEGY_SWITCH_SYNC_TOKEN: ${{ secrets.STRATEGY_SWITCH_SYNC_TOKEN || secrets.RUNTIME_SETTINGS_GH_TOKEN }} |
| 38 | + steps: |
| 39 | + - name: Checkout |
| 40 | + uses: actions/checkout@v6 |
| 41 | + |
| 42 | + - name: Setup Python |
| 43 | + uses: actions/setup-python@v6 |
| 44 | + with: |
| 45 | + python-version: "3.12" |
| 46 | + |
| 47 | + - name: Setup Node.js |
| 48 | + uses: actions/setup-node@v6 |
| 49 | + with: |
| 50 | + node-version: "22" |
| 51 | + |
| 52 | + - name: Regenerate bundled assets |
| 53 | + run: | |
| 54 | + set -euo pipefail |
| 55 | + python3 scripts/sync_strategy_switch_page_asset.py |
| 56 | + git diff --exit-code -- web/strategy-switch-console/page_asset.js web/strategy-switch-console/strategy_profiles_asset.js |
| 57 | +
|
| 58 | + - name: Validate Worker assets |
| 59 | + run: | |
| 60 | + set -euo pipefail |
| 61 | + jq empty web/strategy-switch-console/strategy-profiles.example.json |
| 62 | + node --experimental-default-type=module tests/strategy_switch_worker_validation.mjs |
| 63 | + sed -n '/<script>/,/<\/script>/p' web/strategy-switch-console/index.html | sed '1d;$d' | node --check --input-type=commonjs |
| 64 | + node --check --input-type=module < web/strategy-switch-console/page_asset.js |
| 65 | + node --check --input-type=module < web/strategy-switch-console/strategy_profiles_asset.js |
| 66 | + node --check --input-type=module < web/strategy-switch-console/worker.js |
| 67 | +
|
| 68 | + - name: Prepare Wrangler config |
| 69 | + run: | |
| 70 | + set -euo pipefail |
| 71 | + if [ -z "${CLOUDFLARE_API_TOKEN:-}" ] && [ -z "${CLOUDFLARE_WRANGLER_CONFIG_TOML:-}" ]; then |
| 72 | + echo "CLOUDFLARE_API_TOKEN or CLOUDFLARE_WRANGLER_CONFIG_TOML is required to deploy the strategy switch console." >&2 |
| 73 | + exit 2 |
| 74 | + fi |
| 75 | + if [ -n "${CLOUDFLARE_WRANGLER_CONFIG_TOML:-}" ]; then |
| 76 | + mkdir -p "$HOME/.config/.wrangler/config" |
| 77 | + printf '%s' "$CLOUDFLARE_WRANGLER_CONFIG_TOML" > "$HOME/.config/.wrangler/config/default.toml" |
| 78 | + chmod 600 "$HOME/.config/.wrangler/config/default.toml" |
| 79 | + fi |
| 80 | + if [ -z "${STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID:-}" ]; then |
| 81 | + echo "STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID is required so deploy does not drop the STRATEGY_SWITCH_CONFIG binding." >&2 |
| 82 | + exit 2 |
| 83 | + fi |
| 84 | + python3 - <<'PY' |
| 85 | + import os |
| 86 | + from pathlib import Path |
| 87 | +
|
| 88 | + root = Path(os.environ["WORKER_DIR"]) |
| 89 | + source = root / "wrangler.toml.example" |
| 90 | + target = root / "wrangler.toml" |
| 91 | + text = source.read_text(encoding="utf-8") |
| 92 | + account_id = os.environ.get("CLOUDFLARE_ACCOUNT_ID", "").strip() |
| 93 | + if account_id and "\naccount_id" not in text: |
| 94 | + text = text.replace("\n[vars]\n", f"\naccount_id = \"{account_id}\"\n\n[vars]\n", 1) |
| 95 | +
|
| 96 | + kv_id = os.environ["STRATEGY_SWITCH_CONFIG_KV_NAMESPACE_ID"].strip() |
| 97 | + commented_kv = "\n".join([ |
| 98 | + "# [[kv_namespaces]]", |
| 99 | + "# binding = \"STRATEGY_SWITCH_CONFIG\"", |
| 100 | + "# id = \"replace-with-cloudflare-kv-namespace-id\"", |
| 101 | + "", |
| 102 | + ]) |
| 103 | + active_kv = "\n".join([ |
| 104 | + "[[kv_namespaces]]", |
| 105 | + "binding = \"STRATEGY_SWITCH_CONFIG\"", |
| 106 | + f"id = \"{kv_id}\"", |
| 107 | + "", |
| 108 | + ]) |
| 109 | + if commented_kv in text: |
| 110 | + text = text.replace(commented_kv, active_kv, 1) |
| 111 | + elif 'binding = "STRATEGY_SWITCH_CONFIG"' not in text: |
| 112 | + text = text.rstrip() + "\n\n" + active_kv |
| 113 | + target.write_text(text, encoding="utf-8") |
| 114 | + PY |
| 115 | +
|
| 116 | + - name: Deploy Worker |
| 117 | + working-directory: web/strategy-switch-console |
| 118 | + run: npx wrangler@latest deploy --config wrangler.toml |
| 119 | + |
| 120 | + - name: Sync bundled strategy profiles to KV |
| 121 | + if: github.event_name != 'workflow_dispatch' || inputs.sync_strategy_profiles |
| 122 | + run: | |
| 123 | + set -euo pipefail |
| 124 | + if [ -z "${STRATEGY_SWITCH_CONSOLE_URL:-}" ]; then |
| 125 | + echo "STRATEGY_SWITCH_CONSOLE_URL is required to sync strategy profiles." >&2 |
| 126 | + exit 2 |
| 127 | + fi |
| 128 | + if [ -z "${STRATEGY_SWITCH_SYNC_TOKEN:-}" ]; then |
| 129 | + echo "STRATEGY_SWITCH_SYNC_TOKEN or RUNTIME_SETTINGS_GH_TOKEN is required to sync strategy profiles." >&2 |
| 130 | + exit 2 |
| 131 | + fi |
| 132 | + curl --fail --show-error --silent \ |
| 133 | + --request POST \ |
| 134 | + --header "Authorization: Bearer ${STRATEGY_SWITCH_SYNC_TOKEN}" \ |
| 135 | + --header "Content-Type: application/json" \ |
| 136 | + --data '{"source":"github-actions"}' \ |
| 137 | + "${STRATEGY_SWITCH_CONSOLE_URL%/}/api/internal/sync-strategy-profiles" \ |
| 138 | + | python3 -m json.tool |
0 commit comments