Astro + Cloudflare Pages
Build an Astro site and deploy it to Cloudflare Pages with cache purging.
Overview
This example demonstrates a complete workflow for building and deploying a static Astro website to Cloudflare Pages. The build runs in an Ubuntu container with Node.js installed automatically.
The workflow performs these steps:
- Installs Node.js in the container
- Installs npm dependencies
- Builds the Astro site
- Authenticates with Cloudflare
- Deploys to Cloudflare Pages
- Purges the Cloudflare cache so visitors see the latest content
Build Script
build.csando
// Create a directory reference for the website project.
var website = Directory(".");
// Install Node.js (includes npm) in the Ubuntu container.
Node.Install();
// Install dependencies.
Npm.Install(website);
// Build the Astro site (outputs to ./dist).
Npm.Build(website);
// Verify Cloudflare authentication.
Cloudflare.EnsureAuthenticated();
// Deploy to Cloudflare Pages.
Cloudflare.PagesDeploy(website / "dist", "my-site");
// Purge the Cloudflare cache to ensure visitors see the latest content.
Cloudflare.PurgeCache("example.com");Prerequisites
-
CLOUDFLARE_API_TOKENenvironment variable (or enter when prompted) -
CLOUDFLARE_ACCOUNT_IDenvironment variable (or enter when prompted) - A Cloudflare Pages project already created
Key Operations
| Operation | Purpose |
|---|---|
| Node.Install() | Installs Node.js v22 in the container |
| Npm.Install() | Runs npm install to install dependencies |
| Npm.Build() | Runs npm run build to build the Astro
site |
| Cloudflare.EnsureAuthenticated() | Verifies Cloudflare credentials are available |
| Cloudflare.PagesDeploy() | Deploys the dist folder to Cloudflare
Pages |
| Cloudflare.PurgeCache() | Clears the CDN cache so visitors see the latest content |
Running the Build
shell
andoThe build runs inside a Docker container. On first run, ANDO will prompt for Cloudflare credentials if the environment variables aren't set.