The most useful Astro GitHub Actions workflows are short: one that runs astro check and astro build on every pull request, one that deploys the built dist/ folder to your host when main changes, and a scheduled rebuild for sites that pull content from a CMS. Each is a single YAML file in .github/workflows/, and Astro’s static output means none of them needs a server.
This article gives six workflows you can copy into an Astro 7 project, explains each step, and covers caching, secrets and the mistakes that cause slow or failing runs.
Key Takeaways
- Run
npm ci,astro checkandastro buildon every pull request so broken content never reachesmain.- GitHub Pages deploys need
actions/upload-pages-artifactandactions/deploy-pages, plussiteandbasein the config.- Cache
node_modules/.astroto skip re-optimizing images on every run.- A
scheduletrigger rebuilds a static site so CMS content and scheduled posts go live.- Keep API tokens in repository secrets, never in the workflow file.
Before you start
Every workflow below assumes a few things about the project:
- A lockfile is committed (
package-lock.json,pnpm-lock.yamlorbun.lock), so CI installs exactly what you tested locally. package.jsonhas the usual scripts:"build": "astro build"and, if you type-check,"check": "astro check".- The Node.js version in CI matches Astro’s requirement. Astro 7 needs Node.js 22.12 or later, and 22.23.4 or later from Astro 7.4.2, so pin
node-version: 22or newer (which resolves to the latest 22.x).
astro check needs @astrojs/check and typescript as dev dependencies. Run npx astro check once locally and accept the prompt to install them. If you are setting up deployment for the first time, the deploy guide compares hosts before you commit to one.
1. Check and build every pull request
This is the workflow every Astro project should have. It catches TypeScript errors, invalid frontmatter that fails a collection schema, broken imports, and anything else that stops astro build:
# .github/workflows/ci.yml
name: CI
on:
pull_request:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx astro check
- run: npm run build
cache: npm on setup-node caches the npm download cache between runs, which cuts install time noticeably. npm ci is stricter than npm install: it fails if the lockfile and package.json disagree, which is exactly what you want in CI.
Make the job a required status check in your branch protection settings, and a pull request cannot merge while the build is red.
2. Deploy to GitHub Pages
GitHub Pages hosts static Astro sites for free. Set the repository’s Pages source to GitHub Actions in Settings, then add the deploy workflow:
# .github/workflows/deploy-pages.yml
name: Deploy to GitHub Pages
on:
push:
branches: [main]
workflow_dispatch:
workflow_call:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run build
- uses: actions/upload-pages-artifact@v3
with:
path: dist
deploy:
needs: build
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4
The permissions block is required; deploy-pages authenticates with an OIDC token, which needs id-token: write. workflow_dispatch adds a “Run workflow” button for manual deploys.
The config matters as much as the workflow. For a project site served at username.github.io/repo-name/, set both values:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://username.github.io',
base: '/repo-name',
});
Without base, CSS and links point at the domain root and the site loads unstyled. With a custom domain, drop base and set site to the domain. Astro also maintains withastro/action, which wraps the install and build steps into one; check its README for the current major version if you prefer it. The GitHub Pages deploy guide covers custom domains and 404 pages.
3. Deploy to Cloudflare Pages with Wrangler
Cloudflare Pages can build from Git on its own, but deploying from Actions gives you one pipeline for checks and deploys, and lets you deploy only after tests pass:
# .github/workflows/deploy-cloudflare.yml
name: Deploy to Cloudflare Pages
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run build
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy dist --project-name=my-astro-site
Create an API token in the Cloudflare dashboard with the Pages edit permission, and add it and your account ID as repository secrets under Settings, Secrets and variables, Actions. The same pattern works for Netlify and Vercel with their CLIs. The Cloudflare Pages docs page walks through the project setup on the Cloudflare side.
4. Cache optimized images between runs
Astro stores processed images and other build artifacts in node_modules/.astro (the cacheDir setting). CI starts from a clean checkout, so without a cache every run re-optimizes every image. On an image-heavy site, that can be most of the build time. Add a cache step before the build:
# .github/workflows/ci.yml (build steps)
- run: npm ci
- uses: actions/cache@v4
with:
path: node_modules/.astro
key: astro-${{ runner.os }}-${{ hashFiles('src/assets/**', 'src/content/**') }}
restore-keys: |
astro-${{ runner.os }}-
- run: npm run build
The restore-keys fallback restores the most recent cache even when images changed, so only the new or edited images get processed. Put the cache step after npm ci, because npm ci deletes node_modules before installing. The CI build cache tip has more detail and host-specific variants.
5. Rebuild on a schedule
A static site only changes when it rebuilds. If content comes from a headless CMS, or posts have future pubDate values that should appear on the day, rebuild on a timer:
# .github/workflows/scheduled-rebuild.yml
name: Scheduled rebuild
on:
schedule:
- cron: '0 6 * * *' # every day at 06:00 UTC
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
jobs:
rebuild:
uses: ./.github/workflows/deploy-pages.yml
uses: with a local path calls your existing deploy workflow, so the steps live in one place. That is why the Pages workflow above lists workflow_call under on:, and why the caller grants the same permissions; a called workflow cannot have more than its caller. Note that GitHub runs scheduled workflows only on the default branch, may delay them at busy times, and disables them in repositories with no activity for 60 days.
For content that must appear within seconds of publishing, use your CMS’s webhook to trigger a repository_dispatch event, or your host’s deploy hook, instead of a schedule. The headless CMS guide covers webhook rebuilds.
6. Check links after the build
Broken internal links creep in as pages move. A link checker run against the built HTML catches them before visitors do:
# .github/workflows/links.yml
name: Check links
on:
pull_request:
jobs:
links:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run build
- uses: lycheeverse/lychee-action@v2
with:
args: --offline --no-progress 'dist/**/*.html'
fail: true
--offline checks only internal links and files, so the job is fast and not flaky. Drop it occasionally, or run a separate weekly job, to check external links too, since those break without any change on your side.
Keeping workflows fast and safe
A few habits keep Astro CI cheap and reliable:
- Set
permissionsexplicitly. Give each workflow the least it needs, such ascontents: read. Deploy jobs need more; check jobs do not. - Use
concurrencyon deploys so two pushes in quick succession do not race each other to production. - Pin action versions to a major tag at least, as in the examples above. For stricter supply-chain hygiene, pin to a full commit SHA.
- Keep secrets in secrets. Reference them as
${{ secrets.NAME }}. Workflows triggered by pull requests from forks do not receive secrets, so keep deploy jobs onpushtomain. - Pass build-time variables through
env:on the build step, and declare them inenv.schemaso a missing value fails fast. The environment variables guide covers the schema.
Every Komal theme builds to static files with npm run build, so these workflows work unchanged on a theme project; browse Astro themes if you want a finished site to wire them into. GitHub’s workflow syntax reference and Astro’s GitHub Pages deploy guide are the official references.
FAQ
How do I deploy an Astro site with GitHub Actions?
Build the site with npm ci and npm run build, then upload dist/ with actions/upload-pages-artifact and publish it with actions/deploy-pages. For other hosts, run their CLI, such as Wrangler for Cloudflare, after the build.
Why is my Astro site unstyled on GitHub Pages?
The site is served from a subfolder, but base is not set in astro.config.mjs. Set base: '/repo-name' and site: 'https://username.github.io', then rebuild.
Should I run astro check in CI?
Yes. astro check reports TypeScript and template errors that astro build may not, including type errors in .astro files. It needs @astrojs/check and typescript installed as dev dependencies.
How do I speed up Astro builds in GitHub Actions?
Use cache: npm in actions/setup-node for dependencies, and cache node_modules/.astro with actions/cache so optimized images are reused between runs.
Can GitHub Actions rebuild my Astro site automatically?
Yes. Add a schedule trigger with a cron expression for timed rebuilds, or a repository_dispatch trigger that your CMS calls by webhook when content changes.