Deploy to Cloudflare Pages
此内容尚不支持你的语言。
This guide walks you through deploying the Hermes Agent tutorial site to Cloudflare Pages with a custom domain, automatic CI/CD via GitHub Actions, and production optimizations.
Architecture Overview
Section titled “Architecture Overview”GitHub Push (main branch) │ ▼GitHub Actions (CI/CD) │ ▼pnpm install → astro build → dist/ │ ▼Cloudflare Pages Action │ ▼Cloudflare CDN (Global) │ ▼hermes-agents.netPrerequisites
Section titled “Prerequisites”- A Cloudflare account (free tier works)
- Your domain managed by Cloudflare (or purchase one through Cloudflare)
- GitHub repository with the project code
- Basic familiarity with terminal commands
Step 1: Get Cloudflare API Credentials
Section titled “Step 1: Get Cloudflare API Credentials”1.1 Get API Token
Section titled “1.1 Get API Token”- Log in to Cloudflare Dashboard
- Navigate to My Profile → API Tokens
- Click Create Token
- Use the Edit Cloudflare Workers template
- Configure permissions:
| Permission | Scope |
|---|---|
| Account - Cloudflare Pages - Edit | All accounts |
| Zone - DNS - Edit | All zones |
| Zone - Zone - Read | All zones |
- Click Continue to summary → Create Token
- Copy the token — you’ll only see it once
1.2 Get Account ID
Section titled “1.2 Get Account ID”- In Cloudflare Dashboard, click any domain or go to Websites
- On the right sidebar, find Account ID
- Copy the 32-character hex string
Step 2: Configure GitHub Secrets
Section titled “Step 2: Configure GitHub Secrets”In your GitHub repository, add the two secrets:
- Go to your repository:
https://github.com/bigwei08028/hermes-agent-net - Navigate to Settings → Secrets and variables → Actions
- Click New repository secret and add:
| Secret Name | Value |
|---|---|
CLOUDFLARE_API_TOKEN | Your API token from Step 1.1 |
CLOUDFLARE_ACCOUNT_ID | Your Account ID from Step 1.2 |
Step 3: Create Cloudflare Pages Project
Section titled “Step 3: Create Cloudflare Pages Project”Option A: Via Dashboard (Recommended First Time)
Section titled “Option A: Via Dashboard (Recommended First Time)”- Go to Cloudflare Dashboard → Workers & Pages
- Click Create → Pages → Connect to Git
- Select your GitHub repository
hermes-agent-net - Configure build settings:
| Setting | Value |
|---|---|
| Framework preset | None |
| Build command | pnpm build |
| Build output directory | dist |
| Root directory | / |
- Click Save and Deploy
Option B: Via GitHub Actions (Automatic)
Section titled “Option B: Via GitHub Actions (Automatic)”The project already includes .github/workflows/deploy.yml. Once you push to main with the secrets configured, the workflow will:
- Install Node.js 20 + pnpm 10
- Run
pnpm install --frozen-lockfile - Run
pnpm build(outputs todist/) - Deploy to Cloudflare Pages using
cloudflare/pages-action@v1
The first push will automatically create the Pages project if it doesn’t exist.
Step 4: Configure Custom Domain
Section titled “Step 4: Configure Custom Domain”4.1 Add Domain in Cloudflare Pages
Section titled “4.1 Add Domain in Cloudflare Pages”- Go to Workers & Pages → select
hermes-agents-net - Click Custom domains tab
- Click Set up a custom domain
- Enter
hermes-agents.net - Click Continue
- Select Activate domain (if domain is on Cloudflare) or Enter DNS records manually
4.2 DNS Configuration
Section titled “4.2 DNS Configuration”If your domain is managed by Cloudflare (recommended), the DNS records are added automatically. If not, add these records at your domain registrar:
| Type | Name | Value | Proxy |
|---|---|---|---|
| CNAME | @ | hermes-agents-net.pages.dev | Proxied |
| CNAME | www | hermes-agents-net.pages.dev | Proxied |
4.3 Enable HTTPS
Section titled “4.3 Enable HTTPS”Cloudflare automatically provisions a free SSL certificate. No manual configuration needed.
- Go to SSL/TLS → Overview
- Set encryption mode to Full (strict) for maximum security
- Verify the certificate is active under Edge Certificates
Step 5: Verify Deployment
Section titled “Step 5: Verify Deployment”5.1 Check Build Status
Section titled “5.1 Check Build Status”# In your GitHub repository# Go to Actions tab → "Deploy to Cloudflare Pages" workflow5.2 Verify the Site
Section titled “5.2 Verify the Site”# Check if the site is livecurl -I https://hermes-agents.net
# Expected: HTTP/2 200# Check security headerscurl -I https://hermes-agents.net | grep -i "x-frame\|x-content-type\|referrer"Expected security headers (configured in public/_headers):
X-Frame-Options: DENYX-Content-Type-Options: nosniffReferrer-Policy: strict-origin-when-cross-origin5.3 Verify Sitemap
Section titled “5.3 Verify Sitemap”curl https://hermes-agents.net/sitemap-index.xmlStep 6: Production Optimizations
Section titled “Step 6: Production Optimizations”6.1 Caching Strategy
Section titled “6.1 Caching Strategy”The project already includes caching rules in public/_headers:
# Static assets (JS/CSS/images) - 1 year cache/_astro/* → Cache-Control: public, max-age=31536000, immutable
# Fonts - 1 year cache/fonts/* → Cache-Control: public, max-age=31536000, immutable
# HTML pages - 1 hour cache (for quick updates)/*.html → Cache-Control: public, max-age=36006.2 Cloudflare Cache Rules
Section titled “6.2 Cloudflare Cache Rules”For additional control, configure cache rules in the Cloudflare dashboard:
- Go to Caching → Cache Rules
- Create a rule for Pagefind search index:
| Field | Value |
|---|---|
| Expression | URI Path contains "/pagefind/" |
| Cache TTL | 1 hour |
| Browser TTL | 1 hour |
6.3 Page Rules (Optional)
Section titled “6.3 Page Rules (Optional)”Configure in Rules → Page Rules:
| URL Pattern | Setting | Value |
|---|---|---|
hermes-agents.net/* | Security Level | Medium |
hermes-agents.net/_astro/* | Cache Level | Cache Everything |
hermes-agents.net/pagefind/* | Cache Level | Standard |
Step 7: Cloudflare Analytics
Section titled “Step 7: Cloudflare Analytics”Enable Web Analytics
Section titled “Enable Web Analytics”- Go to Web Analytics in Cloudflare Dashboard
- Click Add a site
- Enter
hermes-agents.net - Copy the JavaScript snippet (optional — Cloudflare also supports beacon-less analytics)
Enable Workers Analytics
Section titled “Enable Workers Analytics”- Go to Workers & Pages → your project → Metrics
- Monitor request counts, error rates, and latency
Understanding the CI/CD Workflow
Section titled “Understanding the CI/CD Workflow”The .github/workflows/deploy.yml file defines the complete pipeline:
name: Deploy to Cloudflare Pages
on: push: branches: [main] # Trigger on push to main workflow_dispatch: # Allow manual trigger
jobs: build-and-deploy: runs-on: ubuntu-latest steps: - Checkout code - Setup Node.js 20 - Install pnpm 10 - Install dependencies (pnpm install --frozen-lockfile) - Build (pnpm build → outputs to dist/) - Deploy to Cloudflare Pages (cloudflare/pages-action@v1)How It Works
Section titled “How It Works”- Trigger: Every push to
mainbranch (or manual trigger) - Build: Astro builds static HTML + CSS + JS to
dist/ - Deploy: Cloudflare Pages Action uploads
dist/to Cloudflare’s global CDN - Propagation: Changes propagate to all 300+ Cloudflare edge locations within seconds
Build Output Structure
Section titled “Build Output Structure”dist/├── index.html # English homepage├── zh-cn/ # Chinese version│ └── index.html├── getting-started/ # Content pages├── features/├── configuration/├── troubleshooting/├── _astro/ # Static assets (hashed for cache busting)├── pagefind/ # Search index├── sitemap-index.xml # Sitemap└── robots.txt # Crawler rulesTroubleshooting
Section titled “Troubleshooting”Build Fails on GitHub Actions
Section titled “Build Fails on GitHub Actions”# Check the workflow logs in GitHub Actions tab# Common causes:# 1. pnpm version mismatch → update pnpm/action-setup version# 2. Node.js version → ensure Node 20# 3. Dependency conflicts → run pnpm install locally firstDeployment Succeeds but Site Shows 404
Section titled “Deployment Succeeds but Site Shows 404”- Check the Pages project name matches
projectNamein the workflow - Verify the
directoryis set todist(notdist/) - Check Cloudflare Pages dashboard for deployment history
Custom Domain Not Working
Section titled “Custom Domain Not Working”# Check DNS propagationdig hermes-agents.net
# Check SSL statuscurl -vI https://hermes-agents.net 2>&1 | grep -E "subject|issuer|expire"
# Verify CNAME recordsnslookup -type=CNAME hermes-agents.netCache Not Updating
Section titled “Cache Not Updating”- Go to Caching → Configuration → Purge Everything
- Or use the API:
curl -X POST "https://api.cloudflare.com/client/v4/zones/<ZONE_ID>/purge_cache" \ -H "Authorization: Bearer <API_TOKEN>" \ -H "Content-Type: application/json" \ -d '{"purge_everything": true}'Search (Pagefind) Not Working After Deploy
Section titled “Search (Pagefind) Not Working After Deploy”Pagefind builds its index during astro build. If search doesn’t work:
- Verify
dist/pagefind/exists in the build output - Check browser console for Pagefind errors
- Purge Cloudflare cache for the
/pagefind/path
Cost Summary
Section titled “Cost Summary”| Service | Cost | Notes |
|---|---|---|
| Cloudflare Pages | Free | Unlimited sites, 500 builds/month |
| Custom Domain | Free | If managed by Cloudflare |
| SSL Certificate | Free | Auto-provisioned |
| Cloudflare CDN | Free | Global edge network |
| Web Analytics | Free | Up to 100K requests/day |
| GitHub Actions | Free | 2000 minutes/month (free tier) |
Total cost: $0/month for a typical documentation site.
Next Steps
Section titled “Next Steps”After deployment is working:
- Submit sitemap to Google Search Console
- Submit sitemap to Bing Webmaster Tools
- Set up Cloudflare Web Analytics
- Configure Cloudflare Email Routing (optional)