A production-ready Cloudflare Worker template with centralized CI/CD pipeline, comprehensive testing, and security best practices.
- ✅ Centralized CI/CD: Uses
batumi-works/cf-pipelinefor consistent deployments - ✅ Testing: Vitest with Miniflare integration for local testing
- ✅ Security: CORS handling, input validation, error boundaries
- ✅ Observability: Datadog integration for deployment tracking
- ✅ Multiple Environments: Production, staging, and development configurations
- ✅ TypeScript Support: Optional TypeScript support with type checking
- ✅ Local Development: Sub-10s local testing with workerd runtime
# Clone this template
git clone https://github.com/batumi-works/cloudflare-worker-template.git my-worker
cd my-worker
# Install dependencies
npm install
# Install Wrangler globally if not already installed
npm install -g wranglerEdit wrangler.toml:
name = "my-worker" # Change this to your worker name
main = "src/index.js"
compatibility_date = "2023-05-18"
[env.production]
name = "my-worker"
routes = [
{ pattern = "myapp.com/*", zone_name = "myapp.com" }
]Edit package.json:
{
"name": "my-worker",
"description": "My awesome Cloudflare Worker"
}# Authenticate with Cloudflare
wrangler auth login
# Test authentication
wrangler whoami# Start local development server
npm run dev
# Run tests
npm run test
# Validate configuration
npm run validate# Deploy to staging
npm run deploy:staging
# Deploy to production
npm run deploy:productionmy-worker/
├── .github/
│ └── workflows/
│ └── ci.yml # 5-line CI/CD configuration
├── src/
│ └── index.js # Worker code
├── test/
│ ├── index.test.js # Test suite
│ └── setup.js # Test setup
├── package.json # Dependencies and scripts
├── wrangler.toml # Cloudflare configuration
├── vitest.config.js # Test configuration
└── README.md # This file
npm run dev- Start local development servernpm run test- Run tests with Miniflarenpm run deploy- Deploy to productionnpm run deploy:staging- Deploy to stagingnpm run deploy:production- Deploy to productionnpm run validate- Validate configurationnpm run lint- Lint code (if configured)npm run typecheck- Type check (if using TypeScript)
The template includes these example endpoints:
GET /- HTML landing pageGET /health- Health check endpointGET /api/hello?name=World- Hello APIGET /api/time- Current time API
Set these secrets using wrangler secret put:
# Example secrets
wrangler secret put CLOUDFLARE_API_TOKEN
wrangler secret put DATABASE_URLConfigure non-sensitive variables in wrangler.toml:
[vars]
ENVIRONMENT = "production"
API_VERSION = "v1"Tests use Vitest with Miniflare for accurate Cloudflare Workers runtime simulation:
# Run tests
npm test
# Run tests in watch mode
npm run test -- --watch
# Run tests with coverage
npm run test -- --coverageThe template includes a minimal 5-line GitHub Actions workflow that uses the centralized pipeline:
jobs:
ci:
uses: batumi-works/cf-pipeline/.github/workflows/ci.yml@v1
# ... configurationAdd these secrets to your GitHub repository:
CLOUDFLARE_API_TOKEN- Cloudflare API token with Workers permissionsDATADOG_API_KEY- (Optional) Datadog API key for observabilityDATADOG_APP_KEY- (Optional) Datadog application key
- Create repository from this template
- Add required secrets in repository settings
- Push to main branch to trigger deployment
- ✅ No hardcoded secrets in code
- ✅ CORS headers configured
- ✅ Input validation and sanitization
- ✅ Error handling and logging
- ✅ Security scanning in CI/CD
- ✅ Dependency vulnerability checks
- ✅ Minimal bundle size
- ✅ ES modules for tree shaking
- ✅ Efficient request routing
- ✅ Caching headers where appropriate
- ✅ Sub-10s local test execution
Local development not starting:
# Check wrangler authentication
wrangler whoami
# Validate configuration
npm run validateTests failing:
# Install test dependencies
npm install
# Check test configuration
cat vitest.config.jsDeployment failing:
# Check wrangler configuration
wrangler validate
# Test deployment dry run
npm run deploy -- --dry-run- Check the troubleshooting guide
- Review Cloudflare Workers documentation
- Open an issue in the template repository
# Install TypeScript dependencies
npm install --save-dev typescript @types/node
# Create tsconfig.json
# Update wrangler.toml main to point to built JS# In wrangler.toml
[[kv_namespaces]]
binding = "MY_KV"
id = "your-kv-namespace-id"
preview_id = "your-preview-kv-namespace-id"# In wrangler.toml
[[durable_objects.bindings]]
name = "MY_DO"
class_name = "MyDurableObject"MIT License - see LICENSE for details.