Repository navigation
178 lines (156 loc) · 7.16 KB
/
Copy pathdeploy.yml
File metadata and controls
178 lines (156 loc) · 7.16 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
name: Deploy to Cloudflare (Worker & Pages)
on:
push:
branches:
- main
- master
paths-ignore:
- '**.md'
- '.gitignore'
workflow_dispatch:
inputs:
deploy_target:
description: 'Deployment Target'
required: true
type: choice
options:
- worker
- pages
default: worker
deploy_version:
description: 'Code Format'
required: true
type: choice
options:
- normal
- encoded
default: normal
create_d1_if_missing:
description: 'Auto-create D1 database if D1_DATABASE_ID secret is not set'
required: true
type: boolean
default: true
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
deploy:
name: Build & Deploy to Cloudflare
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Dependencies
run: |
if [ -f package-lock.json ]; then
npm ci
else
npm install
fi
- name: Validate Source Code
run: npm run build
- name: Setup D1 Database ID
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
D1_DATABASE_ID: ${{ secrets.D1_DATABASE_ID }}
AUTO_CREATE: ${{ github.event_name == 'workflow_dispatch' && inputs.create_d1_if_missing || true }}
run: |
TARGET_ID="${D1_DATABASE_ID}"
# If D1_DATABASE_ID secret is not provided and credentials exist, auto-create or find D1
if [ -z "$TARGET_ID" ] && [ -n "$CLOUDFLARE_API_TOKEN" ] && [ "$AUTO_CREATE" = "true" ]; then
echo "[*] D1_DATABASE_ID secret not set. Checking or creating D1 database 'nahan-db' via Wrangler..."
CREATE_OUTPUT=$(npx wrangler d1 create nahan-db 2>&1 || true)
echo "$CREATE_OUTPUT"
# Extract database_id from output UUID
FOUND_ID=$(echo "$CREATE_OUTPUT" | grep -oE "database_id = \"[0-9a-fA-F-]{36}\"" | cut -d'"' -f2 || true)
if [ -z "$FOUND_ID" ]; then
# Try searching existing databases
FOUND_ID=$(npx wrangler d1 list --json 2>/dev/null | grep -B 2 -A 5 '"name": "nahan-db"' | grep -oE '"uuid": "[0-9a-fA-F-]{36}"' | head -n1 | cut -d'"' -f4 || true)
fi
if [ -n "$FOUND_ID" ]; then
TARGET_ID="$FOUND_ID"
echo "[+] Found/Created D1 Database ID: $TARGET_ID"
fi
fi
if [ -n "$TARGET_ID" ]; then
echo "[*] Injecting D1 Database ID ($TARGET_ID) into wrangler.toml..."
sed -i "s/YOUR_D1_DATABASE_ID_HERE/$TARGET_ID/g" wrangler.toml
sed -i "s/D1_DATABASE_ID_PLACEHOLDER/$TARGET_ID/g" wrangler.toml
else
echo "[!] Warning: No D1_DATABASE_ID specified or created. Database operations will use fallback."
fi
- name: Prepare Encoded Code (if requested)
if: ${{ (github.event_name == 'workflow_dispatch' && inputs.deploy_version == 'encoded') }}
run: |
echo "[*] Bundling and obfuscating code with worker_convertor.py..."
python3 worker_convertor.py encode --bundle -o worker.encoded.js
sed -i 's/main = "worker.js"/main = "worker.encoded.js"/g' wrangler.toml
echo "[+] worker.encoded.js prepared and wrangler.toml updated."
# Deploy as Cloudflare Worker
- name: Deploy Cloudflare Worker
if: ${{ github.event_name != 'workflow_dispatch' || inputs.deploy_target == 'worker' }}
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
# Deploy as Cloudflare Pages
- name: Deploy Cloudflare Pages
if: ${{ github.event_name == 'workflow_dispatch' && inputs.deploy_target == 'pages' }}
run: |
echo "[*] Preparing Cloudflare Pages bundle..."
mkdir -p dist_pages
cp index.html dist_pages/index.html 2>/dev/null || true
cp dashboard.html dist_pages/dashboard.html 2>/dev/null || true
cp subscription.html dist_pages/subscription.html 2>/dev/null || true
if [ "${{ inputs.deploy_version }}" = "encoded" ]; then
cp worker.encoded.js dist_pages/_worker.js
else
# Bundle modular codebase into single _worker.js for Pages
npx wrangler deploy --dry-run --outdir=tmp_bundle
cp tmp_bundle/worker.js dist_pages/_worker.js
rm -rf tmp_bundle
fi
# Configure wrangler.toml for Pages: remove main and add pages_build_output_dir
sed -i '/^main\s*=/d' wrangler.toml
sed -i '/^name =/a pages_build_output_dir = "dist_pages"' wrangler.toml
# Ensure project exists
npx wrangler pages project create nahan-gateway --production-branch=main || true
echo "[*] Deploying to Cloudflare Pages..."
npx wrangler pages deploy dist_pages --project-name=nahan-gateway
# Automatically bind D1 to Pages project via REST API if database ID is known
if [ -n "${TARGET_ID}" ] && [ -n "${CLOUDFLARE_API_TOKEN}" ] && [ -n "${CLOUDFLARE_ACCOUNT_ID}" ]; then
echo "[*] Binding D1 database ${TARGET_ID} to Pages project via REST API..."
curl -s -X PATCH "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects/nahan-gateway" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{\"deployment_configs\":{\"production\":{\"d1_databases\":{\"IOT_DB\":{\"id\":\"${TARGET_ID}\"}},\"compatibility_flags\":[\"nodejs_compat\"]},\"preview\":{\"d1_databases\":{\"IOT_DB\":{\"id\":\"${TARGET_ID}\"}},\"compatibility_flags\":[\"nodejs_compat\"]}}}" || true
fi
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Sync Optional Worker Secrets
if: ${{ secrets.BOT_TOKEN != '' && (github.event_name != 'workflow_dispatch' || inputs.deploy_target == 'worker') }}
run: |
echo "[*] Setting BOT_TOKEN secret..."
echo "${{ secrets.BOT_TOKEN }}" | npx wrangler secret put BOT_TOKEN || true
if [ -n "${{ secrets.ADMIN_CHAT_ID }}" ]; then
echo "${{ secrets.ADMIN_CHAT_ID }}" | npx wrangler secret put ADMIN_CHAT_ID || true
fi
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}