forked from maillab/cloud-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (110 loc) · 4.92 KB
/
deploy-cloudflare.yml
File metadata and controls
132 lines (110 loc) · 4.92 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
name: 🚀 Deploy cloud-mail to Cloudflare Workers
on:
push:
branches: [ main ]
paths:
- "mail-worker/**"
- "mail-vue/**"
workflow_dispatch:
jobs:
Deploy-cloud-mail:
name: 🏗️ Build and Deploy
runs-on: ubuntu-latest
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
D1_DATABASE_ID: ${{ secrets.D1_DATABASE_ID }}
KV_NAMESPACE_ID: ${{ secrets.KV_NAMESPACE_ID }}
R2_BUCKET_NAME: ${{ secrets.R2_BUCKET_NAME }}
DOMAIN: ${{ secrets.DOMAIN }}
ADMIN: ${{ secrets.ADMIN }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
INIT_URL: ${{ secrets.INIT_URL }}
outputs:
deployment_skipped: ${{ steps.deploy.outputs.deployment_skipped }}
steps:
- name: ➡️ Checkout repository
uses: actions/checkout@v4
- name: 📦 Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
cache-dependency-path: "./mail-worker/package-lock.json"
- name: 📥 Install dependencies
run: npm ci
working-directory: ./mail-worker
- name: 📡 Disable wrangler telemetry
working-directory: ./mail-worker
run: npx wrangler telemetry disable -c wrangler-action.toml
- name: 🤫 Set Worker secrets (ignore if already exists)
working-directory: ./mail-worker
run: |
WORKER_NAME="cloud-mail"
CONFIG_FILE="wrangler-action.toml"
echo "🔒 Attempting to create/update secrets using '$CONFIG_FILE'."
for VAR in DOMAIN ADMIN JWT_SECRET; do
if [ -n "${!VAR}" ]; then
VAR_LOWER=$(echo "$VAR" | tr '[:upper:]' '[:lower:]')
echo ">> Processing secret: '$VAR_LOWER'"
(echo "${!VAR}" | npx wrangler secret put "$VAR_LOWER" --name "$WORKER_NAME" -c "$CONFIG_FILE") || true
else
echo "⚠️ Warning: GitHub Secret '$VAR' is not set. Skipping."
fi
done
echo "✨ Secret processing complete."
- name: 🛠️ Prepare Config and Deploy
id: deploy
working-directory: ./mail-worker
run: |
if [ -z "$D1_DATABASE_ID" ] || [ -z "$KV_NAMESPACE_ID" ] || [ -z "$R2_BUCKET_NAME" ]; then
echo "⚠️ Required secrets (D1_DATABASE_ID, KV_NAMESPACE_ID, or R2_BUCKET_NAME) are not set."
echo "🟡 Skipping deployment."
echo "deployment_skipped=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "deployment_skipped=false" >> $GITHUB_OUTPUT
CONFIG_FILE="wrangler-action.toml"
echo "⚙️ Dynamically updating '$CONFIG_FILE' with binding IDs..."
sed -i "s|\${D1_DATABASE_ID}|${D1_DATABASE_ID}|g" "$CONFIG_FILE"
sed -i "s|\${KV_NAMESPACE_ID}|${KV_NAMESPACE_ID}|g" "$CONFIG_FILE"
sed -i "s|\${R2_BUCKET_NAME}|${R2_BUCKET_NAME}|g" "$CONFIG_FILE"
echo "🚀 Configuration updated. Starting deployment..."
npx wrangler deploy -c "$CONFIG_FILE" | grep -v "https://.*\.workers\.dev" || true
echo "✅ Deployment command executed."
- name: 🗄️ Initialize Database (if INIT_URL is set)
if: steps.deploy.outputs.deployment_skipped == 'false'
run: |
if [ -z "$INIT_URL" ]; then
echo "✅ Deployment successful. INIT_URL not set, skipping initialization."
exit 0
fi
echo "⏳ Waiting 10 秒之前 before checking initialization status..."
sleep 10
HTTP_CODE=$(curl -s -w "%{http_code}" -o response.txt "$INIT_URL")
RESPONSE_BODY=$(cat response.txt)
echo "🔎 Checking response... (Status: $HTTP_CODE)"
if [ "$HTTP_CODE" = "200" ] && [ "$RESPONSE_BODY" = "初始化成功" ]; then
echo "🎉✅ Fresh initialization successful!"
elif [ "$HTTP_CODE" = "200" ]; then
echo "✅ Database is already initialized or in a stable state. Response: $RESPONSE_BODY"
else
echo "⚠️ Database initialization check failed with HTTP status: $HTTP_CODE. Please check your worker logs."
fi
- name: 📣 Notify Final Status
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
if [ "${{ steps.deploy.outputs.deployment_skipped }}" == "true" ]; then
echo "🟡 Deployment was skipped due to missing configuration."
else
echo "🎉🎉🎉 Hooray! Deployment completed successfully! 🎉🎉🎉"
fi
else
echo "❌❌❌ Oh no! The deployment failed. Please check the logs above for errors. ❌❌❌"
fi
- name: Delete workflow runs
uses: GitRML/delete-workflow-runs@main
with:
retain_days: '3'
keep_minimum_runs: '0'