Update _worker.js #104
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Cloudflare Workers | |
on: | |
workflow_dispatch: | |
inputs: | |
environment: | |
description: 'wrangler environment to deploy to' | |
required: true | |
default: 'dev' | |
type: choice | |
options: | |
- dev | |
- prod | |
commit: | |
description: 'Git commit to deploy' | |
default: 'main' | |
required: true | |
push: | |
branches: | |
- "main" | |
repository_dispatch: | |
env: | |
GIT_REF: ${{ github.event.inputs.commit || github.ref }} | |
WORKERS_ENV: ${{ github.event.inputs.environment || 'dev' }} | |
jobs: | |
deploy: | |
name: Deploy workers | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
with: | |
ref: ${{ env.GIT_REF }} | |
- name: Install Wrangler | |
run: npm install -g wrangler | |
- name: Set Cloudflare Environment Variables | |
run: | | |
echo "CLOUDFLARE_ACCOUNT_ID=${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" >> $GITHUB_ENV | |
echo "CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }}" >> $GITHUB_ENV | |
- name: Validate Cloudflare Credentials | |
run: | | |
curl -X GET "https://api.cloudflare.com/client/v4/accounts" \ | |
-H "Authorization: Bearer ${{ secrets.CLOUDFLARE_API_TOKEN }}" \ | |
-H "Content-Type: application/json" | |
- name: Create or Use Existing KV Namespace | |
id: create_or_find_kv | |
run: | | |
echo "Listing KV namespaces" | |
namespace_output=$(wrangler kv namespace list) | |
echo "Namespace list output: $namespace_output" | |
existing_namespace_id=$(echo "$namespace_output" | jq -r '.[] | select(.title == "oai_global_variables" or .title == "worker-oai_global_variables" or .title == "oaifreehelper-oai_global_variables" or .title == "oaifreehelper-oai_global_variables_preview" or .title == "worker-oai_global_variables_preview") | .id' | head -n 1) | |
if [ -z "$existing_namespace_id" ]; then | |
echo "No existing KV namespace found, creating a new one." | |
namespace_creation_output=$(wrangler kv namespace create "oai_global_variables") || exit 1 | |
echo "Namespace creation output: $namespace_creation_output" | |
namespace_id=$(echo "$namespace_creation_output" | grep -oP '(?<=id = ")[^"]+') | |
echo "CF_KV_NAMESPACE_ID=$namespace_id" >> $GITHUB_ENV | |
else | |
echo "Found existing KV namespace with ID: $existing_namespace_id" | |
echo "CF_KV_NAMESPACE_ID=$existing_namespace_id" >> $GITHUB_ENV | |
fi | |
- name: Generate wrangler.toml for main worker | |
run: | | |
echo "name = \"oaifreehelper\"" > wrangler.toml | |
echo "workers_dev = true" >> wrangler.toml | |
echo "main = \"_worker.js\"" >> wrangler.toml | |
echo "compatibility_date = \"2024-06-01\"" >> wrangler.toml | |
echo "[[kv_namespaces]]" >> wrangler.toml | |
echo "binding = \"oai_global_variables\"" >> wrangler.toml | |
echo "id = \"$CF_KV_NAMESPACE_ID\"" >> wrangler.toml | |
- name: Publish main worker to Cloudflare Workers | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
run: wrangler deploy | |
- name: Generate wrangler.toml for api worker | |
run: | | |
echo "name = \"api\"" > wrangler.toml | |
echo "workers_dev = true" >> wrangler.toml | |
echo "main = \"api_worker.js\"" >> wrangler.toml | |
echo "compatibility_date = \"2024-06-01\"" >> wrangler.toml | |
echo "[[kv_namespaces]]" >> wrangler.toml | |
echo "binding = \"oai_global_variables\"" >> wrangler.toml | |
echo "id = \"$CF_KV_NAMESPACE_ID\"" >> wrangler.toml | |
- name: Publish api worker to Cloudflare Workers | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
run: wrangler deploy | |
- name: Generate wrangler.toml for free worker | |
run: | | |
echo "name = \"free\"" > wrangler.toml | |
echo "workers_dev = true" >> wrangler.toml | |
echo "main = \"free_worker.js\"" >> wrangler.toml | |
echo "compatibility_date = \"2024-06-01\"" >> wrangler.toml | |
echo "[[kv_namespaces]]" >> wrangler.toml | |
echo "binding = \"oai_global_variables\"" >> wrangler.toml | |
echo "id = \"$CF_KV_NAMESPACE_ID\"" >> wrangler.toml | |
- name: Publish free worker to Cloudflare Workers | |
env: | |
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
run: wrangler deploy | |
- name: Generate wrangler.toml for voice worker | |
run: | | |
echo "name = \"voice\"" > wrangler.toml | |
echo "workers_dev = true" >> wrangler.toml | |
echo "main = \"voice_worker.js\"" >> wrangler.toml | |
echo "compatibility_date = \"2024-06-01\"" >> wrangler.toml | |
- name: Publish api worker to Cloudflare Workers | |
run: wrangler deploy |