feat: 优化一键部署到 Cloudflare Worker

This commit is contained in:
nap0o committed 2026-03-13 23:17:05 +08:00
1 parent c634f0727e
commit a06e595f7b
6 files changed
+76 -14

No files matched your search

+40 -3
View File
@@ -16,6 +16,41 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Check Required Secrets
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
ENCRYPTION_KEY: ${{ secrets.ENCRYPTION_KEY }}
OAUTH_ALLOWED_USERS: ${{ secrets.OAUTH_ALLOWED_USERS }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_D1_DATABASE_ID: ${{ secrets.CLOUDFLARE_D1_DATABASE_ID }}
run: |
REQUIRED_VARS=(
"JWT_SECRET"
"ENCRYPTION_KEY"
"OAUTH_ALLOWED_USERS"
"CLOUDFLARE_API_TOKEN"
"CLOUDFLARE_ACCOUNT_ID"
"CLOUDFLARE_D1_DATABASE_ID"
)
MISSING_VARS=()
for VAR in "${REQUIRED_VARS[@]}"; do
if [ -z "${!VAR}" ]; then
MISSING_VARS+=("$VAR")
fi
done
if [ ${#MISSING_VARS[@]} -ne 0 ]; then
echo "❌ Error: The following required secrets are missing or empty:"
for VAR in "${MISSING_VARS[@]}"; do
echo " - $VAR"
done
echo "Please configure them in your repository's Settings > Secrets and variables > Actions."
exit 1
fi
echo "✅ All required secrets are present."
- name: Setup Node.js
uses: actions/setup-node@v4
with:
@@ -46,9 +81,11 @@ jobs:
CLOUDFLARE_D1_DATABASE_ID: ${{ secrets.CLOUDFLARE_D1_DATABASE_ID }}
CLOUDFLARE_D1_DATABASE_NAME: ${{ secrets.CLOUDFLARE_D1_DATABASE_NAME || '2fauth-db' }}
run: |
# 仅替换第一个匹配到的 database_id (对应生产环境配置),保留 dev 环境配置
sed -i "0,/database_id =/s/database_id = \".*\"/database_id = \"$CLOUDFLARE_D1_DATABASE_ID\"/" "wrangler.toml"
sed -i "0,/database_name =/s/database_name = \".*\"/database_name = \"$CLOUDFLARE_D1_DATABASE_NAME\"/" "wrangler.toml"
sed -i '/^\[\[d1_databases\]\]/d; /^[[:space:]]*database_name[[:space:]]*=/d; /^[[:space:]]*database_id[[:space:]]*=/d' wrangler.toml
if grep -q "\[env.dev\]" wrangler.toml; then
sed -i '/\[env.dev\]/,$d' wrangler.toml
fi
printf "\n[[d1_databases]]\nbinding = \"DB\"\ndatabase_name = \"$CLOUDFLARE_D1_DATABASE_NAME\"\ndatabase_id = \"$CLOUDFLARE_D1_DATABASE_ID\"\n" >> wrangler.toml
# 更新数据库结构
- name: Update Database Schema
+18 -4
View File
@@ -60,10 +60,12 @@
这是最省心的方式,完全不需要自己的服务器,由 Cloudflare 免费为你托管。
#### 1. 一键全自动部署
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/nap0o/2fauth-worker)
1. Fork 本项目仓库,发财小手点`Star`! ⭐
2. 点击下方部署按钮 <br />
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://dash.cloudflare.com/?to=/:account/workers-and-pages/create)
#### 2. 注意事项与授权指引
* 在部署向导中,你需要授权 Cloudflare 读取该仓库(系统会自动在你的 GitHub 账号下创建一个私有克隆)。
#### 2. 授权指引
* 在部署向导中,你需要授权 Cloudflare 读取该仓库。
<details>
<summary>点击查看:授权指引示意图</summary>
@@ -72,7 +74,19 @@
<img height="400" src="https://github.com/user-attachments/assets/c1f2d5ee-2f3f-47c2-969f-00308cadff21" />
</details>
#### 3. 添加如下变量和机密
#### 3. 部署流程
依次点击:Continue with GitHub -> 选择你 Fork 后的仓库(2fauth-worker)-> 下一步 -> 部署 -> 继续处理项目 -> 设置
<details>
<summary>点击查看:详细部署步骤示意图</summary>
<img height="400" src="https://github.com/user-attachments/assets/6a933580-98d5-4b09-ac1f-e2aa33380807" /><br />
<img height="400" src="https://github.com/user-attachments/assets/14e57427-0eac-4d20-8d9c-f8957803f247" /><br />
<img height="400" src="https://github.com/user-attachments/assets/b123a063-4671-4fc2-94fc-94b7a2d71235" /><br />
<img height="600" src="https://github.com/user-attachments/assets/c4be75c1-2732-4dfb-abaa-72384f482d4f" />
<img height="300" src="https://github.com/user-attachments/assets/ddce1191-353c-466d-a55c-044a1fcc47b4" />
</details>
#### 4. 在`设置`添加如下变量和机密
* `ENCRYPTION_KEY`:32位以上随机密钥。
* `JWT_SECRET`:32位以上随机JWT密钥。
* `OAUTH_ALLOWED_USERS`:你的邮箱@example.com
+17 -3
View File
@@ -62,10 +62,12 @@ Whether you're leveraging Cloudflare's free tier or deploying on your own NAS/mi
The most hassle-free way. No server required; Cloudflare hosts it for you for free.
#### 1. One-click Auto Deploy
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/nap0o/2fauth-worker)
1. Fork this repository, and give it a `Star`! ⭐
2. Click the deploy button below <br />
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://dash.cloudflare.com/?to=/:account/workers-and-pages/create)
#### 2. Notes & Authorization
* During the deployment wizard, you'll need to authorize Cloudflare to read this repository (it will automatically create a private fork in your account).
* In the deployment wizard, you need to authorize Cloudflare to read this repository.
<details>
<summary>Click to view: Authorization Guide Diagrams</summary>
@@ -74,7 +76,19 @@ The most hassle-free way. No server required; Cloudflare hosts it for you for fr
<img height="400" src="https://github.com/user-attachments/assets/c1f2d5ee-2f3f-47c2-969f-00308cadff21" />
</details>
#### 3. Add the following Variables and Secrets
#### 3. Deployment Flow
Click in sequence: Continue with GitHub -> Select your forked repository (2fauth-worker) -> Next -> Deploy -> Continue to project -> Settings
<details>
<summary>Click to view: Detailed deployment steps diagram</summary>
<img height="400" src="https://github.com/user-attachments/assets/6a933580-98d5-4b09-ac1f-e2aa33380807" /><br />
<img height="400" src="https://github.com/user-attachments/assets/14e57427-0eac-4d20-8d9c-f8957803f247" /><br />
<img height="400" src="https://github.com/user-attachments/assets/b123a063-4671-4fc2-94fc-94b7a2d71235" /><br />
<img height="600" src="https://github.com/user-attachments/assets/c4be75c1-2732-4dfb-abaa-72384f482d4f" />
<img height="300" src="https://github.com/user-attachments/assets/ddce1191-353c-466d-a55c-044a1fcc47b4" />
</details>
#### 4. Add the following Variables and Secrets in `Settings`
* `ENCRYPTION_KEY`: A random key with at least 32 characters.
* `JWT_SECRET`: A random JWT secret with at least 32 characters.
* `OAUTH_ALLOWED_USERS`: your_email@example.com
-1
View File
@@ -14,7 +14,6 @@ command = "npm run build --prefix frontend"
[[d1_databases]]
binding = "DB"
database_name = "2fauth-db-demo"
database_id = "082530be-db1b-4346-b4f2-dbeeb6305c51"
[triggers]
crons = ["0 2 * * *"]
-1
View File
@@ -15,7 +15,6 @@ not_found_handling = "single-page-application"
[build]
command = "npm run build --prefix ../frontend"
[[d1_databases]]
binding = "DB"
database_name = "2fauth-db-dev"
+1 -2
View File
@@ -14,7 +14,6 @@ command = "npm run build --prefix frontend"
[[d1_databases]]
binding = "DB"
database_name = "2fauth-db"
database_id = "9e591736-5dbb-4f09-8769-0270d7257731"
[triggers]
crons = ["0 2 * * *"]
@@ -23,4 +22,4 @@ crons = ["0 2 * * *"]
[[env.dev.d1_databases]]
binding = "DB"
database_name = "2fauth-db-dev"
database_id = "9e591736-5dbb-4f09-8769-0270d7257731"
database_id = "9e591736-5dbb-4f09-8769-0270d7257731"