- Astro Framework
@astrojs/cloudflare
adapter
- Cloudflare account
- D1 Database
- GitHub OAuth App
- GitHub Access Token
- GitHub Webhook
- Stripe
- Environment Variables
- Secrets
Create a Cloudflare account.
You will need a paid plan and enable Workers for Platforms1.
Create a Cloudflare API Token with permission to Edit Workers Scripts.
Add CLOUDFLARE_ACCOUNT_ID
and CLOUDFLARE_API_TOKEN
as project secrets.
Create a Cloudflare D1 Database for managing user and worker data.
From monorepo root:
pnpm --filter codius-astro d1 create <your-d1-db-name>
Add the D1 database_name
and database_id
to [[wrangler.env]]
in wrangler.toml in the following sections:
[[d1_databases]]
(for local development)[[env.preview.d1_databases]]
(for preview deployments)[[env.production.d1_databases]]
(for production deployments)
See Workers Environments and Pages Branch deployment controls documentation for more information about managing production and preview environments for your Cloudflare Pages project.
Apply migrations to your database:
pnpm --filter codius-astro drizzle-kit generate
pnpm --filter codius-astro d1 migrations apply <your-d1-db-name> --local
pnpm --filter codius-astro d1 migrations apply <your-d1-db-name> --remote
Note: Rollback local migrations by deleting the state files with the following command:
rm packages/codius-astro/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/*
You'll need to create a GitHub OAuth App to allow users to login with GitHub.
Authorization callback URL
should be either:
http://localhost:8788/login/github/callback
for local development orhttps://<your-pages-domain>/login/github/callback
Example:
Note: Use
127.0.0.1:8788
if running withpnpm run dev
instead ofpnpm run preview
Add GITHUB_APP_NAME
and GITHUB_CLIENT_ID
as environment variables with the values from the GitHub OAuth App.
Add GITHUB_CLIENT_SECRET
as a project secret with the value from the GitHub OAuth App.
You'll need to create a GitHub Access Token with Read and Write access to Actions in order for codius-astro to deploy workers via its GitHub Actions workflow.
Add GITHUB_ACCESS_TOKEN
as a project secret with the value from the GitHub Access Token.
You'll need to create a GitHub repository webhook to handle Workflow jobs and Workflows runs events.
For Payload URL
, use either:
https://smee.io/<your-smee-path>
for local development with Smee.io orhttps://<your-pages-domain>/webhooks/github/workflow-job
Generate a Secret
for the webhook and add GITHUB_WEBHOOK_SECRET
as a project secret.
Example:
You'll need to create a Stripe account and get your Stripe Secret Key.
Create a Stripe Product and Price for topping up worker balances.
The Price should be of type Customer chooses price
.
Add STRIPE_SECRET_KEY
and STRIPE_TOPUP_PRICE_ID
as project secrets with the values from Stripe.
Environment variables are managed in wrangler.toml.
The namespace of the dispatch-worker deployed to Cloudflare Workers.
The hostname of the dispatch-worker deployed to Cloudflare Workers.
The name of the GitHub OAuth App.
The Client ID from the GitHub OAuth App.
Secrets are managed in .dev.vars
for local development.
From packages/codius-astro
:
cp .example.dev.vars .dev.vars
Add the secrets from your .dev.vars
to your Cloudflare Pages project as encrypted secrets.
CLOUDFLARE_ACCOUNT_ID
CLOUDFLARE_API_TOKEN
GITHUB_CLIENT_SECRET
GITHUB_ACCESS_TOKEN
GITHUB_WEBHOOK_SECRET
STRIPE_SECRET_KEY
STRIPE_TOPUP_PRICE_ID
Your Cloudflare Account ID.
Your Cloudflare API Token.
The Client Secret from the GitHub OAuth App.
The GitHub Access Token for deploying workers.
The secret for the GitHub repository webhook.
Your Stripe Secret Key.
The Stripe Price ID for topping up your account.
You can deploy the codius-astro site to Cloudflare Pages via either direct upload or git integration.
For direct upload, you can use the following command from the monorepo root:
pnpm --filter codius-astro pages deploy
All commands are run from the root of the monorepo, from a terminal:
Command | Action |
---|---|
pnpm install |
Installs dependencies |
pnpm --filter codius-astro run dev |
Starts local dev server at localhost:8788 |
pnpm --filter codius-astro run build |
Build your production site to ./dist/ |
pnpm --filter codius-astro run preview |
Preview your build locally, before deploying |
pnpm --filter codius-astro run astro ... |
Run CLI commands like astro add , astro check |
pnpm --filter codius-astro run astro -- --help |
Get help using the Astro CLI |
pnpm --filter codius-astro run smee |
Start a local webhook proxy with Smee.io |
Footnotes
-
Cloudflare
wrangler
cli currently doesn't support local development for Workers for Platforms. See https://github.com/cloudflare/workers-sdk/pull/5622 ↩