|
| 1 | +# Deployment Guide for Gist Blog |
| 2 | + |
| 3 | +## 📌 Overview |
| 4 | + |
| 5 | +This guide provides comprehensive instructions for deploying your Gist Blog using Cloudflare Workers while maintaining security and flexibility. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +1. [Prerequisites](#prerequisites) |
| 10 | +2. [Initial Setup](#initial-setup) |
| 11 | +3. [Configuration](#configuration) |
| 12 | +4. [Deployment](#deployment) |
| 13 | +5. [Custom Domains](#custom-domains) |
| 14 | +6. [Advanced Configuration](#advanced-configuration) |
| 15 | +7. [Troubleshooting](#troubleshooting) |
| 16 | +8. [Maintenance](#maintenance) |
| 17 | +9. [Security Best Practices](#security-best-practices) |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +### Required Accounts |
| 22 | + |
| 23 | +1. **GitHub Account** |
| 24 | + - Personal access token with `gist` scope |
| 25 | + - Create at: https://github.com/settings/tokens/new |
| 26 | + - Use minimal permissions: `public_repo` or `gist` |
| 27 | + |
| 28 | +2. **Cloudflare Account** |
| 29 | + - Free tier is sufficient |
| 30 | + - Sign up at: https://cloudflare.com |
| 31 | + |
| 32 | +### Local Requirements |
| 33 | + |
| 34 | +```bash |
| 35 | +# Check versions |
| 36 | +node --version # v16.0.0 or higher |
| 37 | +npm --version # v7.0.0 or higher |
| 38 | +go version # go1.19 or higher (for CLI tools) |
| 39 | + |
| 40 | +# Install Wrangler CLI globally |
| 41 | +npm install -g wrangler |
| 42 | +``` |
| 43 | + |
| 44 | +## Initial Setup |
| 45 | + |
| 46 | +### 1. Clone Repository |
| 47 | + |
| 48 | +```bash |
| 49 | +git clone https://github.com/yourusername/gist-blog.git |
| 50 | +cd gist-blog |
| 51 | +``` |
| 52 | + |
| 53 | +### 2. Install Dependencies |
| 54 | + |
| 55 | +```bash |
| 56 | +npm install |
| 57 | +``` |
| 58 | + |
| 59 | +### 3. Configure Environment |
| 60 | + |
| 61 | +```bash |
| 62 | +# Option 1: Environment Variables |
| 63 | +export GITHUB_USER="your-github-username" |
| 64 | +export GITHUB_TOKEN="ghp_your_token_here" |
| 65 | + |
| 66 | +# Option 2: .env file (DO NOT COMMIT) |
| 67 | +echo 'GITHUB_USER=your-github-username' > .env |
| 68 | +echo 'GITHUB_TOKEN=ghp_your_token_here' >> .env |
| 69 | +``` |
| 70 | + |
| 71 | +### 4. Authenticate with Cloudflare |
| 72 | + |
| 73 | +```bash |
| 74 | +wrangler login |
| 75 | +# Opens browser for authentication |
| 76 | +``` |
| 77 | + |
| 78 | +## Configuration |
| 79 | + |
| 80 | +### Update wrangler.toml |
| 81 | + |
| 82 | +Edit `wrangler.toml` with your settings: |
| 83 | + |
| 84 | +```toml |
| 85 | +name = "my-gist-blog" # Your worker name |
| 86 | +main = "worker.js" |
| 87 | +compatibility_date = "2024-01-01" |
| 88 | + |
| 89 | +[vars] |
| 90 | +GITHUB_USER = "your-github-username" |
| 91 | + |
| 92 | +# KV namespace will be configured automatically |
| 93 | +[[kv_namespaces]] |
| 94 | +binding = "GIST_CACHE" |
| 95 | +id = "will-be-replaced-after-creation" |
| 96 | +preview_id = "will-be-replaced-after-creation" |
| 97 | +``` |
| 98 | + |
| 99 | +### Create KV Namespace |
| 100 | + |
| 101 | +```bash |
| 102 | +# Create production namespace |
| 103 | +wrangler kv namespace create "GIST_CACHE" |
| 104 | + |
| 105 | +# Create preview namespace for development |
| 106 | +wrangler kv namespace create "GIST_CACHE" --preview |
| 107 | + |
| 108 | +# Update wrangler.toml with both namespace IDs |
| 109 | +``` |
| 110 | + |
| 111 | +### Add GitHub Token (Optional) |
| 112 | + |
| 113 | +For extended GitHub API access or private gists: |
| 114 | + |
| 115 | +```bash |
| 116 | +wrangler secret put GITHUB_TOKEN |
| 117 | +# Paste token when prompted (hidden input) |
| 118 | +``` |
| 119 | + |
| 120 | +## Deployment |
| 121 | + |
| 122 | +### Development Testing |
| 123 | + |
| 124 | +```bash |
| 125 | +# Start local development server |
| 126 | +npm run dev |
| 127 | +# or |
| 128 | +wrangler dev |
| 129 | + |
| 130 | +# Visit http://localhost:8787 |
| 131 | +``` |
| 132 | + |
| 133 | +### Production Deployment |
| 134 | + |
| 135 | +```bash |
| 136 | +# Deploy to Cloudflare Workers |
| 137 | +npm run deploy |
| 138 | +# or |
| 139 | +wrangler deploy |
| 140 | + |
| 141 | +# Output will show your deployment URL |
| 142 | +# Example: https://my-gist-blog.username.workers.dev |
| 143 | +``` |
| 144 | + |
| 145 | +## Custom Domains |
| 146 | + |
| 147 | +### Option 1: Subdomain (Recommended) |
| 148 | + |
| 149 | +1. Add to `wrangler.toml`: |
| 150 | + ```toml |
| 151 | + [[routes]] |
| 152 | + pattern = "blog.yourdomain.com/*" |
| 153 | + zone_name = "yourdomain.com" |
| 154 | + ``` |
| 155 | + |
| 156 | +2. Create DNS record in Cloudflare: |
| 157 | + - Type: CNAME |
| 158 | + - Name: blog |
| 159 | + - Target: my-gist-blog.username.workers.dev |
| 160 | + - Proxy: ON (orange cloud) |
| 161 | + |
| 162 | +3. Deploy: |
| 163 | + ```bash |
| 164 | + wrangler deploy |
| 165 | + ``` |
| 166 | + |
| 167 | +### Option 2: Root Domain |
| 168 | + |
| 169 | +1. Add to `wrangler.toml`: |
| 170 | + ```toml |
| 171 | + [[routes]] |
| 172 | + pattern = "yourdomain.com/*" |
| 173 | + zone_name = "yourdomain.com" |
| 174 | + ``` |
| 175 | + |
| 176 | +2. Deploy: |
| 177 | + ```bash |
| 178 | + wrangler deploy |
| 179 | + ``` |
| 180 | + |
| 181 | +### Option 3: Multiple Domains |
| 182 | + |
| 183 | +```toml |
| 184 | +[[routes]] |
| 185 | +pattern = "blog.domain1.com/*" |
| 186 | +zone_name = "domain1.com" |
| 187 | + |
| 188 | +[[routes]] |
| 189 | +pattern = "domain2.com/*" |
| 190 | +zone_name = "domain2.com" |
| 191 | +``` |
| 192 | + |
| 193 | +## Advanced Configuration |
| 194 | + |
| 195 | +### Enhanced Worker Features |
| 196 | + |
| 197 | +Use `worker-enhanced.js` for additional features: |
| 198 | + |
| 199 | +```toml |
| 200 | +main = "worker-enhanced.js" # RSS, sitemap, better SEO |
| 201 | +``` |
| 202 | + |
| 203 | +Features: |
| 204 | +- RSS feed at `/rss.xml` |
| 205 | +- Sitemap at `/sitemap.xml` |
| 206 | +- Enhanced meta tags |
| 207 | +- Post excerpts |
| 208 | + |
| 209 | +### Cache and Performance Configuration |
| 210 | + |
| 211 | +```javascript |
| 212 | +// Modify in worker.js |
| 213 | +this.CACHE_TTL = 300; // 5 minutes (default) |
| 214 | +this.ITEMS_PER_PAGE = 10; // Posts per page |
| 215 | + |
| 216 | +// Examples of customization: |
| 217 | +this.CACHE_TTL = 3600; // 1 hour cache |
| 218 | +this.ITEMS_PER_PAGE = 20; // 20 posts per page |
| 219 | +``` |
| 220 | + |
| 221 | +## Troubleshooting |
| 222 | + |
| 223 | +### Common Issues |
| 224 | + |
| 225 | +1. **KV Namespace Errors** |
| 226 | + ```bash |
| 227 | + wrangler kv namespace create "GIST_CACHE" |
| 228 | + ``` |
| 229 | + |
| 230 | +2. **Route Conflicts** |
| 231 | + ```bash |
| 232 | + wrangler route list |
| 233 | + # Remove conflicting routes via Cloudflare dashboard |
| 234 | + ``` |
| 235 | + |
| 236 | +3. **Authentication Problems** |
| 237 | + ```bash |
| 238 | + wrangler logout |
| 239 | + wrangler login |
| 240 | + ``` |
| 241 | + |
| 242 | +### Debugging |
| 243 | + |
| 244 | +```bash |
| 245 | +# Real-time logs |
| 246 | +wrangler tail |
| 247 | + |
| 248 | +# List deployments |
| 249 | +wrangler deployments |
| 250 | + |
| 251 | +# Test worker |
| 252 | +curl https://your-blog.workers.dev |
| 253 | +``` |
| 254 | + |
| 255 | +## Maintenance |
| 256 | + |
| 257 | +### Regular Tasks |
| 258 | + |
| 259 | +```bash |
| 260 | +# Check outdated packages |
| 261 | +npm outdated |
| 262 | + |
| 263 | +# Update dependencies |
| 264 | +npm update wrangler |
| 265 | +npm update |
| 266 | + |
| 267 | +# Sync gists (if CLI tool available) |
| 268 | +./gist-manager sync |
| 269 | + |
| 270 | +# Backup cache |
| 271 | +cp -r .gist-cache .gist-cache.backup |
| 272 | +``` |
| 273 | + |
| 274 | +### Rollback Deployment |
| 275 | + |
| 276 | +```bash |
| 277 | +# List deployments |
| 278 | +wrangler deployments list |
| 279 | + |
| 280 | +# Rollback to specific version |
| 281 | +wrangler rollback [version-id] |
| 282 | +``` |
| 283 | + |
| 284 | +## Security Best Practices |
| 285 | + |
| 286 | +1. **Never Commit Secrets** |
| 287 | + ```bash |
| 288 | + echo ".env" >> .gitignore |
| 289 | + echo "*.token" >> .gitignore |
| 290 | + ``` |
| 291 | + |
| 292 | +2. **Use Cloudflare Secrets** |
| 293 | + ```bash |
| 294 | + wrangler secret put GITHUB_TOKEN |
| 295 | + wrangler secret put ADMIN_KEY |
| 296 | + ``` |
| 297 | + |
| 298 | +3. **Token Management** |
| 299 | + - Create new tokens periodically |
| 300 | + - Update via `wrangler secret put` |
| 301 | + - Revoke old tokens |
| 302 | + |
| 303 | +4. **Monitor Access** |
| 304 | + ```bash |
| 305 | + wrangler tail --format json | grep "status" |
| 306 | + ``` |
| 307 | + |
| 308 | +## Next Steps |
| 309 | + |
| 310 | +1. Create first post |
| 311 | +2. Customize styling |
| 312 | +3. Set up automation |
| 313 | +4. Add analytics |
| 314 | + |
| 315 | +## Support |
| 316 | + |
| 317 | +- **Issues**: [GitHub Issues](https://github.com/yourusername/gist-blog/issues) |
| 318 | +- **Cloudflare Docs**: [workers.cloudflare.com](https://workers.cloudflare.com) |
| 319 | +- **Wrangler CLI**: [Wrangler Documentation](https://developers.cloudflare.com/workers/cli-wrangler) |
0 commit comments