cloudflare-pages
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloudflare Pages
Cloudflare Pages
Deploy any static site to Cloudflare Pages — create projects, attach custom domains, wire up CI/CD, and verify deployments. Framework-agnostic: works with Hugo, Astro, Next.js, Nuxt, SvelteKit, or any build tool that produces static output.
将任意静态站点部署到Cloudflare Pages——创建项目、绑定自定义域名、配置CI/CD并验证部署。框架无关:支持Hugo、Astro、Next.js、Nuxt、SvelteKit或任何可生成静态输出的构建工具。
Prerequisites
前置条件
| Requirement | How to get it |
|---|---|
| wrangler CLI | |
| Cloudflare account | dash.cloudflare.com — free tier works |
| Account ID | Dashboard → any zone → Overview sidebar, or |
| API Token (CI/CD) | Dashboard → API Tokens → "Edit Cloudflare Workers" template (includes Pages) |
| DNS API Token | Dashboard → API Tokens → Custom → Zone:DNS:Edit (only if adding custom domains via API) |
| Zone ID | Dashboard → your domain → Overview sidebar (only for custom domains) |
| 要求 | 获取方式 |
|---|---|
| wrangler CLI | |
| Cloudflare 账号 | dash.cloudflare.com — 免费套餐可用 |
| 账号ID | 控制台 → 任意区域 → 侧边栏概览,或执行 |
| API Token(CI/CD用) | 控制台 → API Tokens → “Edit Cloudflare Workers” 模板(包含Pages权限) |
| DNS API Token | 控制台 → API Tokens → 自定义 → Zone:DNS:Edit(仅当通过API添加自定义域名时需要) |
| 区域ID | 控制台 → 你的域名 → 侧边栏概览(仅自定义域名需要) |
Quick Deploy
快速部署
For a one-off manual deploy when CI/CD isn't set up yet:
bash
undefined当尚未配置CI/CD时,可使用以下一次性手动部署方式:
bash
undefined1. Build your site
1. 构建你的站点
<your-build-command> # e.g. hugo --minify, npm run build
<your-build-command> # 例如 hugo --minify, npm run build
2. Deploy output directory
2. 部署输出目录
wrangler pages deploy <output-dir> --project-name <project-name>
Each deploy gets a unique preview URL. The latest deploy on `production` branch becomes the live site at `<project>.pages.dev`.wrangler pages deploy <output-dir> --project-name <project-name>
每次部署都会生成唯一的预览URL。`production`分支上的最新部署会成为`<project>.pages.dev`上的正式站点。Full Setup Workflow
完整设置流程
Step 1 — Create the Pages project
步骤1 — 创建Pages项目
bash
bash scripts/setup_pages_project.sh <project-name> "<build-command>" <output-dir> [branch]This runs and prints the resulting URL. The argument defaults to .
wrangler pages project create*.pages.devbranchmainManual alternative:
bash
wrangler pages project create <project-name> --production-branch mainbash
bash scripts/setup_pages_project.sh <project-name> "<build-command>" <output-dir> [branch]该脚本会执行并输出生成的 URL。参数默认值为。
wrangler pages project create*.pages.devbranchmain手动替代方案:
bash
wrangler pages project create <project-name> --production-branch mainStep 2 — Deploy
步骤2 — 部署
Build and deploy:
bash
<your-build-command>
wrangler pages deploy <output-dir> --project-name <project-name>构建并部署:
bash
<your-build-command>
wrangler pages deploy <output-dir> --project-name <project-name>Step 3 — Add a custom domain
步骤3 — 添加自定义域名
bash
undefinedbash
undefinedSet required environment variables
设置必要的环境变量
export CLOUDFLARE_ACCOUNT_ID="<your-account-id>"
export CLOUDFLARE_API_TOKEN="<pages-edit-token>"
export CLOUDFLARE_ZONE_ID="<your-zone-id>"
export CLOUDFLARE_DNS_TOKEN="<dns-edit-token>" # optional, falls back to API_TOKEN
bash scripts/add_custom_domain.sh <custom-domain> <project-name>
The script performs two operations:
1. **Registers** the domain on the Pages project via Cloudflare API
2. **Creates** a proxied CNAME DNS record pointing to `<project>.pages.dev`
Both operations are idempotent — safe to re-run.export CLOUDFLARE_ACCOUNT_ID="<your-account-id>"
export CLOUDFLARE_API_TOKEN="<pages-edit-token>"
export CLOUDFLARE_ZONE_ID="<your-zone-id>"
export CLOUDFLARE_DNS_TOKEN="<dns-edit-token>" # 可选,若未设置则回退使用API_TOKEN
bash scripts/add_custom_domain.sh <custom-domain> <project-name>
该脚本执行两项操作:
1. **注册**:通过Cloudflare API将域名绑定到Pages项目
2. **创建**:指向`<project>.pages.dev`的代理CNAME DNS记录
两项操作均为幂等操作——可安全重复执行。Step 4 — Set up CI/CD
步骤4 — 配置CI/CD
See references/ci-cd-templates.md for complete GitHub Actions workflows for Hugo, Astro, Next.js, and generic npm builds.
Required GitHub Secrets:
| Secret | Value |
|---|---|
| API token with "Edit Cloudflare Workers" permissions |
| Your Cloudflare account ID |
Set these at: Repository → Settings → Secrets and variables → Actions → New repository secret
查看**references/ci-cd-templates.md** 获取适用于Hugo、Astro、Next.js和通用npm构建的完整GitHub Actions工作流。
必需的GitHub Secrets:
| 密钥 | 值 |
|---|---|
| 拥有“Edit Cloudflare Workers”权限的API令牌 |
| 你的Cloudflare账号ID |
在以下路径设置:仓库 → 设置 → 机密与变量 → Actions → 新建仓库机密
Rollback
回滚部署
bash
undefinedbash
undefinedList recent deployments
列出最近的部署记录
wrangler pages deployment list --project-name <project-name>
wrangler pages deployment list --project-name <project-name>
Rollback: checkout previous commit, rebuild, redeploy
回滚:切换到之前的提交,重新构建并部署
git checkout <commit-hash>
<your-build-command>
wrangler pages deploy <output-dir> --project-name <project-name>
git checkout <commit-hash>
<your-build-command>
wrangler pages deploy <output-dir> --project-name <project-name>
Return to latest
回到最新版本
git checkout main
undefinedgit checkout main
undefinedVerify Deployment
验证部署
bash
export CLOUDFLARE_ACCOUNT_ID="<your-account-id>"
export CLOUDFLARE_API_TOKEN="<your-token>"
bash scripts/verify_deployment.sh <project-name> [custom-domain]Shows: latest deployment status (color-coded), last 5 deployments table, custom domain SSL status, and HTTP 200 check on .
*.pages.devbash
export CLOUDFLARE_ACCOUNT_ID="<your-account-id>"
export CLOUDFLARE_API_TOKEN="<your-token>"
bash scripts/verify_deployment.sh <project-name> [custom-domain]显示内容:最新部署状态(带颜色标记)、最近5次部署表格、自定义域名SSL状态,以及对的HTTP 200检查结果。
*.pages.devScript Reference
脚本参考
| Script | Purpose | Required env vars |
|---|---|---|
| Create Pages project via wrangler | |
| Register domain + create CNAME | |
| Check deploy status, SSL, HTTP | |
| 脚本 | 用途 | 必需的环境变量 |
|---|---|---|
| 通过wrangler创建Pages项目 | |
| 注册域名 + 创建CNAME记录 | |
| 检查部署状态、SSL、HTTP情况 | |
Token Reference
令牌参考
| Operation | Token needed | Scope | Where used |
|---|---|---|---|
| Wrangler OAuth | Automatic via | Local CLI, CI/CD |
| Register custom domain on Pages | API Token | Account:Cloudflare Pages:Edit | |
| Create CNAME DNS record | DNS API Token | Zone:DNS:Edit | |
| CI/CD deploy (GitHub Actions) | API Token | "Edit Cloudflare Workers" template | GitHub Secrets |
| Query deployments | API Token | Account:Cloudflare Pages:Read | |
Tip: For simpler setups, a single API token with both Pages:Edit and DNS:Edit scopes can be used — set and omit .
CLOUDFLARE_API_TOKENCLOUDFLARE_DNS_TOKEN| 操作 | 需要的令牌 | 权限范围 | 使用场景 |
|---|---|---|---|
| Wrangler OAuth | 自动通过 | 本地CLI、CI/CD |
| 在Pages上注册自定义域名 | API Token | Account:Cloudflare Pages:Edit | |
| 创建CNAME DNS记录 | DNS API Token | Zone:DNS:Edit | |
| CI/CD部署(GitHub Actions) | API Token | “Edit Cloudflare Workers”模板 | GitHub Secrets |
| 查询部署记录 | API Token | Account:Cloudflare Pages:Read | |
提示: 对于更简单的设置,可使用同时拥有Pages:Edit和DNS:Edit权限的单个API令牌——设置并省略即可。
CLOUDFLARE_API_TOKENCLOUDFLARE_DNS_TOKENTroubleshooting
故障排除
| Problem | Cause | Fix |
|---|---|---|
| wrangler not installed | |
| OAuth expired or wrong token | |
| Custom domain stuck "pending" | Missing or wrong CNAME record | Check DNS: CNAME must point to |
| SSL not provisioning | Domain not proxied through Cloudflare | Set CNAME proxy to "Proxied" (orange cloud) |
| 522 error on custom domain | DNS record exists but wrong target | Delete old record, re-run |
| Deploy succeeds but site 404s | Wrong output directory | Verify build output dir matches deploy path |
| CI/CD deploy fails | Missing GitHub secrets | Add |
| 问题 | 原因 | 解决方法 |
|---|---|---|
| 未安装wrangler | 执行 |
| OAuth过期或令牌错误 | 执行 |
| 自定义域名显示“pending”(待处理) | 缺少或错误的CNAME记录 | 检查DNS:CNAME必须指向 |
| SSL证书无法配置 | 域名未通过Cloudflare代理 | 将CNAME的代理设置为“Proxied”(橙色云朵图标) |
| 自定义域名出现522错误 | DNS记录存在但目标错误 | 删除旧记录,重新运行 |
| 部署成功但站点返回404 | 输出目录错误 | 验证构建输出目录与部署路径是否一致 |
| CI/CD部署失败 | 缺少GitHub Secrets | 添加 |
Anti-Patterns
反模式
- Don't use Cloudflare's git integration — use for direct uploads. Git integration adds complexity with no benefit for this workflow.
wrangler pages deploy - Don't create multiple projects for the same site — one project can have multiple custom domains.
- Don't skip the CNAME step — registering a domain on Pages without the DNS record leaves it "pending" indefinitely.
- Don't use unproxied CNAME records — Cloudflare's SSL and CDN only work when the orange cloud (proxy) is enabled.
- Don't hardcode account IDs in scripts — always use environment variables.
- 不要使用Cloudflare的Git集成 —— 使用进行直接上传。Git集成会增加复杂度,且在此工作流中无额外收益。
wrangler pages deploy - 不要为同一站点创建多个项目 —— 一个项目可绑定多个自定义域名。
- 不要跳过CNAME步骤 —— 仅在Pages上注册域名而不添加DNS记录会导致状态一直停留在“pending”。
- 不要使用未代理的CNAME记录 —— 只有启用橙色云朵(代理)时,Cloudflare的SSL和CDN才能正常工作。
- 不要在脚本中硬编码账号ID —— 始终使用环境变量。