-
Notifications
You must be signed in to change notification settings - Fork 292
124 lines (107 loc) · 4.81 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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