本项目已配置 GitHub Actions 自动部署工作流,可在代码推送到 main 分支后自动部署到 Cloudflare Workers,保持您的 Worker 代码与 GitHub 仓库同步。
| 特性 | 一键部署 | 自动部署(GitHub Actions) |
|---|---|---|
| 初次部署 | ✅ 快速便捷 | ⚙️ 需要配置 |
| 代码同步 | ❌ 需手动重新部署 | ✅ 自动同步 |
| 持续维护 | ❌ 不适合 | ✅ 推荐 |
| 适用场景 | 快速体验 | 长期使用 |
核心区别:
- 一键部署:只在首次点击按钮时部署,之后 GitHub 代码更新不会自动同步到 Worker
- 自动部署:每次 push 代码后自动部署,保持 Worker 与 GitHub 仓库同步
-
访问 Cloudflare API Tokens 页面
https://dash.cloudflare.com/profile/api-tokens -
创建新 Token
- 点击右上角 Create Token 按钮
- 选择 Edit Cloudflare Workers 模板
-
配置 Token 权限
在模板页面中,确保以下权限已配置:
设置项 配置值 Permissions Account → Workers Scripts → Edit Account Resources Include → 选择你的账户 Zone Resources All zones (或根据需要选择) TTL 建议留空(永久有效) -
生成并保存 Token
- 点击 Continue to summary
- 检查配置无误后,点击 Create Token
⚠️ 重要:复制生成的 Token(只显示一次,请妥善保存)
示例:wL4R8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-
访问 Cloudflare Dashboard
https://dash.cloudflare.com/ -
获取 Account ID
- 点击左侧菜单 Workers & Pages
- 在右侧栏会显示 Account ID
- 点击复制图标复制 Account ID
示例:a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6
-
打开仓库 Settings
https://github.com/你的用户名/cf-ghproxy-worker/settings/secrets/actions或者在仓库页面:
- 点击 Settings 标签
- 左侧菜单选择 Secrets and variables → Actions
-
添加 CLOUDFLARE_API_TOKEN
- 点击 New repository secret
- Name:
CLOUDFLARE_API_TOKEN - Secret: 粘贴步骤 1 中获取的 API Token
- 点击 Add secret
-
添加 CLOUDFLARE_ACCOUNT_ID
- 再次点击 New repository secret
- Name:
CLOUDFLARE_ACCOUNT_ID - Secret: 粘贴步骤 2 中获取的 Account ID
- 点击 Add secret
-
验证配置
配置完成后,你应该在 Secrets 列表中看到:
Name Updated CLOUDFLARE_API_TOKEN now CLOUDFLARE_ACCOUNT_ID now
重要:确保 wrangler.toml 中的 Worker 名称与 Cloudflare 上已部署的 Worker 名称一致。
-
查看
wrangler.toml配置name = "github-proxy" # 这是你的 Worker 名称
-
检查 Cloudflare 上的 Worker 名称
- 访问 https://dash.cloudflare.com/
- 进入 Workers & Pages
- 查看已部署的 Worker 名称
-
如果名称不一致
方法 A:修改
wrangler.toml(推荐)name = "你在Cloudflare上的Worker名称"
方法 B:在 Cloudflare Dashboard 中重命名 Worker
- 点击 Worker 名称
- Settings → Rename
- 改为
github-proxy
配置完成后,以下操作会自动触发部署:
修改以下文件并 push 到 main 分支:
worker.jswrangler.toml.github/workflows/deploy.yml
# 修改代码后
git add worker.js
git commit -m "feat: update worker code"
git push origin main
# GitHub Actions 会自动开始部署- 打开仓库的 Actions 标签
- 选择左侧 Deploy to Cloudflare Workers workflow
- 点击右侧 Run workflow 下拉菜单
- 选择
main分支 - 点击 Run workflow 按钮
-
进入 Actions 页面
https://github.com/你的用户名/cf-ghproxy-worker/actions -
查看 Workflow 运行
- 绿色 ✅:部署成功
- 黄色 🟡:正在运行
- 红色 ❌:部署失败
-
查看详细日志
- 点击任意 workflow 运行
- 点击 Deploy job
- 展开各个步骤查看详细日志
-
部署成功示例
✅ Checkout repository ✅ Deploy to Cloudflare Workers Published github-proxy (1.23s) https://github-proxy.your-subdomain.workers.dev
Workflow 文件位于:.github/workflows/deploy.yml
name: Deploy to Cloudflare Workers
on:
push:
branches:
- main # 监听 main 分支的 push
paths:
- 'worker.js' # 只在这些文件改变时触发
- 'wrangler.toml'
- '.github/workflows/deploy.yml'
workflow_dispatch: # 允许手动触发监听更多文件:
paths:
- 'worker.js'
- 'wrangler.toml'
- 'package.json' # 添加更多文件
- 'src/**' # 监听整个目录监听多个分支:
branches:
- main
- develop
- production排除文件:
paths-ignore:
- '**.md' # 忽略所有 Markdown 文件
- 'docs/**' # 忽略文档目录如果不想使用 GitHub Actions,也可以使用 Wrangler CLI 手动部署。
# 使用 npm
npm install -g wrangler
# 使用 yarn
yarn global add wrangler
# 使用 pnpm
pnpm add -g wranglerwrangler login这会打开浏览器窗口,按提示完成授权。
# 在项目目录下
wrangler deploy
# 输出示例:
# Total Upload: 12.34 KiB / gzip: 5.67 KiB
# Uploaded github-proxy (1.23 sec)
# Published github-proxy (2.34 sec)
# https://github-proxy.your-subdomain.workers.dev# 查看日志
wrangler tail
# 本地开发
wrangler dev
# 查看部署列表
wrangler deployments list
# 回滚到上一个版本
wrangler rollback
# 查看 Worker 详情
wrangler whoamiA: 一键部署只是初始化部署,并不会在后续保持同步。要实现自动同步,必须按照本文档配置 GitHub Actions。
A: 可能的原因:
-
未配置 GitHub Secrets
- 检查是否添加了
CLOUDFLARE_API_TOKEN和CLOUDFLARE_ACCOUNT_ID - 在仓库 Settings → Secrets and variables → Actions 中验证
- 检查是否添加了
-
Workflow 未触发
- 检查修改的文件是否在
paths监听列表中 - 查看 Actions 标签页,确认是否有 workflow 运行
- 检查修改的文件是否在
-
Workflow 运行失败
- 进入 Actions 标签页查看详细错误日志
- 常见错误:API Token 权限不足、Account ID 错误、Worker 名称不匹配
-
Worker 名称不匹配
- 检查
wrangler.toml中的name是否与 Cloudflare 上的 Worker 名称一致
- 检查
A: 按照以下步骤验证:
# 1. 修改 worker.js,添加一行注释
echo "// Test auto-deployment" >> worker.js
# 2. 提交并推送
git add worker.js
git commit -m "test: verify auto-deployment"
git push origin main
# 3. 立即查看 Actions 页面
# 应该看到一个新的 workflow 运行
# 4. 等待部署完成(约 1-2 分钟)
# 5. 访问你的 Worker URL,检查是否更新A: 可以!编辑 .github/workflows/deploy.yml:
on:
push:
branches:
- main
paths:
- 'worker.js' # 只监听这些文件
- 'wrangler.toml'
# 添加或删除文件路径A: 三种方法:
-
删除 workflow 文件
git rm .github/workflows/deploy.yml git commit -m "ci: disable auto-deployment" git push -
禁用 workflow(保留文件)
- 进入仓库 Actions 标签页
- 选择 "Deploy to Cloudflare Workers"
- 点击右侧
...→ Disable workflow
-
修改触发条件
on: workflow_dispatch: # 只保留手动触发
A: 是的,但对于大多数项目足够:
| 账户类型 | 免费额度 |
|---|---|
| 公开仓库 | 无限制 |
| 私有仓库(免费账户) | 2,000 分钟/月 |
| 私有仓库(Pro账户) | 3,000 分钟/月 |
每次部署约消耗 0.5-1 分钟。
Error: Authentication error (10000)
原因:API Token 无效或权限不足
解决方案:
- 重新生成 API Token(确保选择 "Edit Cloudflare Workers" 模板)
- 更新 GitHub Secret
CLOUDFLARE_API_TOKEN - 确认 Token 包含
Account → Workers Scripts → Edit权限
Error: Account ID is required
原因:未配置 CLOUDFLARE_ACCOUNT_ID 或 Account ID 错误
解决方案:
- 在 Cloudflare Dashboard 的 Workers & Pages 页面复制正确的 Account ID
- 检查 GitHub Secret
CLOUDFLARE_ACCOUNT_ID是否正确 - 确保没有多余的空格或换行符
Error: Worker "github-proxy" not found
原因:wrangler.toml 中的 Worker 名称与 Cloudflare 上的不一致
解决方案:
- 检查
wrangler.toml中的name字段 - 在 Cloudflare Dashboard 中确认实际的 Worker 名称
- 修改其中一个使其一致
Error: Script size exceeds the limit
原因:Worker 代码文件大小超过限制(免费版 1MB,付费版 10MB)
解决方案:
- 优化代码,移除不必要的注释和空格
- 使用模块化拆分代码
- 考虑升级到 Workers Paid 计划
原因:Workflow 卡住或超时
解决方案:
- 进入 Actions 页面,点击运行中的 workflow
- 点击右上角 "Cancel workflow" 取消
- 检查 workflow 日志,查找错误
- 修复问题后重新触发
原因:可能是缓存问题
解决方案:
# 1. 清除浏览器缓存或使用无痕模式访问
# 2. 在 Cloudflare Dashboard 清除缓存
# Caching → Configuration → Purge Cache
# 3. 验证 Worker 代码
# 在 Cloudflare Dashboard → Workers & Pages → 点击 Worker 名称
# → Quick Edit 查看实际代码- GitHub Actions 官方文档
- Cloudflare Wrangler Action
- Cloudflare Workers 文档
- Wrangler CLI 文档
- Cloudflare API Tokens
- Introduction
- Why Auto-Deployment
- Configuration Steps
- Trigger Auto-Deployment
- View Deployment Status
- Workflow Configuration
- Manual Deployment (Alternative)
- FAQ
- Troubleshooting
This project has a GitHub Actions auto-deployment workflow configured to automatically deploy to Cloudflare Workers when code is pushed to the main branch, keeping your Worker code synced with the GitHub repository.
| Feature | One-Click Deploy | Auto-Deployment (GitHub Actions) |
|---|---|---|
| Initial Deployment | ✅ Quick & Easy | ⚙️ Requires Setup |
| Code Sync | ❌ Manual Re-deploy Needed | ✅ Automatic Sync |
| Ongoing Maintenance | ❌ Not Suitable | ✅ Recommended |
| Use Case | Quick Trial | Long-term Use |
Key Difference:
- One-Click Deploy: Only deploys when button is clicked, GitHub code updates will NOT sync automatically
- Auto-Deployment: Deploys automatically on every push, keeping Worker synced with GitHub repository
-
Visit Cloudflare API Tokens Page
https://dash.cloudflare.com/profile/api-tokens -
Create New Token
- Click Create Token button in the top right
- Select Edit Cloudflare Workers template
-
Configure Token Permissions
In the template page, ensure the following permissions are configured:
Setting Configuration Permissions Account → Workers Scripts → Edit Account Resources Include → Select your account Zone Resources All zones (or select as needed) TTL Recommended to leave empty (permanent) -
Generate and Save Token
- Click Continue to summary
- Verify configuration, then click Create Token
⚠️ Important: Copy the generated token (shown only once, save it securely)
Example: wL4R8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-
Visit Cloudflare Dashboard
https://dash.cloudflare.com/ -
Get Account ID
- Click Workers & Pages in the left menu
- Account ID will be displayed on the right sidebar
- Click the copy icon to copy the Account ID
Example: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6
-
Open Repository Settings
https://github.com/your-username/cf-ghproxy-worker/settings/secrets/actionsOr in the repository page:
- Click Settings tab
- Select Secrets and variables → Actions in the left menu
-
Add CLOUDFLARE_API_TOKEN
- Click New repository secret
- Name:
CLOUDFLARE_API_TOKEN - Secret: Paste the API Token from Step 1
- Click Add secret
-
Add CLOUDFLARE_ACCOUNT_ID
- Click New repository secret again
- Name:
CLOUDFLARE_ACCOUNT_ID - Secret: Paste the Account ID from Step 2
- Click Add secret
-
Verify Configuration
After configuration, you should see in the Secrets list:
Name Updated CLOUDFLARE_API_TOKEN now CLOUDFLARE_ACCOUNT_ID now
Important: Ensure the Worker name in wrangler.toml matches the Worker name deployed on Cloudflare.
-
Check
wrangler.tomlConfigurationname = "github-proxy" # This is your Worker name
-
Check Worker Name on Cloudflare
- Visit https://dash.cloudflare.com/
- Go to Workers & Pages
- Check the deployed Worker name
-
If Names Don't Match
Method A: Modify
wrangler.toml(Recommended)name = "your-actual-worker-name-on-cloudflare"
Method B: Rename Worker in Cloudflare Dashboard
- Click Worker name
- Settings → Rename
- Change to
github-proxy
After configuration, the following actions will automatically trigger deployment:
Modify the following files and push to main branch:
worker.jswrangler.toml.github/workflows/deploy.yml
# After modifying code
git add worker.js
git commit -m "feat: update worker code"
git push origin main
# GitHub Actions will automatically start deployment- Open repository Actions tab
- Select Deploy to Cloudflare Workers workflow on the left
- Click Run workflow dropdown on the right
- Select
mainbranch - Click Run workflow button
-
Go to Actions Page
https://github.com/your-username/cf-ghproxy-worker/actions -
View Workflow Runs
- Green ✅: Deployment successful
- Yellow 🟡: Running
- Red ❌: Deployment failed
-
View Detailed Logs
- Click any workflow run
- Click Deploy job
- Expand each step to view detailed logs
-
Successful Deployment Example
✅ Checkout repository ✅ Deploy to Cloudflare Workers Published github-proxy (1.23s) https://github-proxy.your-subdomain.workers.dev
Workflow file is located at: .github/workflows/deploy.yml
name: Deploy to Cloudflare Workers
on:
push:
branches:
- main # Listen to push on main branch
paths:
- 'worker.js' # Trigger only when these files change
- 'wrangler.toml'
- '.github/workflows/deploy.yml'
workflow_dispatch: # Allow manual triggerMonitor More Files:
paths:
- 'worker.js'
- 'wrangler.toml'
- 'package.json' # Add more files
- 'src/**' # Monitor entire directoryMonitor Multiple Branches:
branches:
- main
- develop
- productionExclude Files:
paths-ignore:
- '**.md' # Ignore all Markdown files
- 'docs/**' # Ignore docs directoryIf you don't want to use GitHub Actions, you can manually deploy using Wrangler CLI.
# Using npm
npm install -g wrangler
# Using yarn
yarn global add wrangler
# Using pnpm
pnpm add -g wranglerwrangler loginThis will open a browser window for authorization.
# In project directory
wrangler deploy
# Example output:
# Total Upload: 12.34 KiB / gzip: 5.67 KiB
# Uploaded github-proxy (1.23 sec)
# Published github-proxy (2.34 sec)
# https://github-proxy.your-subdomain.workers.dev# View logs
wrangler tail
# Local development
wrangler dev
# View deployment list
wrangler deployments list
# Rollback to previous version
wrangler rollback
# View Worker details
wrangler whoamiA: One-click deploy is only for initial deployment and doesn't maintain sync afterwards. To enable auto-sync, you must configure GitHub Actions as described in this guide.
A: Possible reasons:
-
GitHub Secrets Not Configured
- Check if
CLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_IDare added - Verify in Repository Settings → Secrets and variables → Actions
- Check if
-
Workflow Not Triggered
- Check if modified files are in the
pathswatch list - Check Actions tab to confirm workflow run
- Check if modified files are in the
-
Workflow Run Failed
- Go to Actions tab to view detailed error logs
- Common errors: insufficient API Token permissions, incorrect Account ID, Worker name mismatch
-
Worker Name Mismatch
- Check if
nameinwrangler.tomlmatches Worker name on Cloudflare
- Check if
A: Follow these steps:
# 1. Modify worker.js, add a comment
echo "// Test auto-deployment" >> worker.js
# 2. Commit and push
git add worker.js
git commit -m "test: verify auto-deployment"
git push origin main
# 3. Immediately check Actions page
# Should see a new workflow run
# 4. Wait for deployment to complete (about 1-2 minutes)
# 5. Visit your Worker URL to check if updatedA: Yes! Edit .github/workflows/deploy.yml:
on:
push:
branches:
- main
paths:
- 'worker.js' # Monitor only these files
- 'wrangler.toml'
# Add or remove file pathsA: Three methods:
-
Delete Workflow File
git rm .github/workflows/deploy.yml git commit -m "ci: disable auto-deployment" git push -
Disable Workflow (keep file)
- Go to repository Actions tab
- Select "Deploy to Cloudflare Workers"
- Click
...on the right → Disable workflow
-
Modify Trigger Conditions
on: workflow_dispatch: # Keep only manual trigger
A: Yes, but sufficient for most projects:
| Account Type | Free Quota |
|---|---|
| Public Repos | Unlimited |
| Private Repos (Free) | 2,000 minutes/month |
| Private Repos (Pro) | 3,000 minutes/month |
Each deployment costs about 0.5-1 minute.
Error: Authentication error (10000)
Cause: Invalid API Token or insufficient permissions
Solution:
- Regenerate API Token (ensure "Edit Cloudflare Workers" template is selected)
- Update GitHub Secret
CLOUDFLARE_API_TOKEN - Verify Token contains
Account → Workers Scripts → Editpermission
Error: Account ID is required
Cause: CLOUDFLARE_ACCOUNT_ID not configured or incorrect
Solution:
- Copy correct Account ID from Cloudflare Dashboard's Workers & Pages page
- Verify GitHub Secret
CLOUDFLARE_ACCOUNT_IDis correct - Ensure no extra spaces or line breaks
Error: Worker "github-proxy" not found
Cause: Worker name in wrangler.toml doesn't match Cloudflare
Solution:
- Check
namefield inwrangler.toml - Verify actual Worker name in Cloudflare Dashboard
- Modify one to match the other
Error: Script size exceeds the limit
Cause: Worker code size exceeds limit (Free: 1MB, Paid: 10MB)
Solution:
- Optimize code, remove unnecessary comments and whitespace
- Use modular code splitting
- Consider upgrading to Workers Paid plan
Cause: Workflow stuck or timed out
Solution:
- Go to Actions page, click running workflow
- Click "Cancel workflow" in top right
- Check workflow logs for errors
- Fix issues and re-trigger
Cause: Likely cache issue
Solution:
# 1. Clear browser cache or use incognito mode
# 2. Purge cache in Cloudflare Dashboard
# Caching → Configuration → Purge Cache
# 3. Verify Worker code
# In Cloudflare Dashboard → Workers & Pages → Click Worker name
# → Quick Edit to view actual code
