🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Astro Webhook Rebuild: Redeploy When CMS Content Changes

Set up an Astro webhook rebuild so publishing in your headless CMS triggers a new deploy: direct deploy hooks, GitHub repository_dispatch, and debouncing.

#Astro#Deployment#Webhook Rebuild#Headless CMS

An Astro webhook rebuild connects your headless CMS to your host so that publishing an entry starts a new build: you paste the host’s deploy hook URL into the CMS’s webhook settings, and the CMS sends a POST request to it whenever content is published. For more control, point the webhook at GitHub’s repository_dispatch API and run the build in GitHub Actions, where you can filter and debounce events.

Key Takeaways

  • A static Astro site only shows new CMS content after a rebuild.
  • The simplest setup is CMS webhook to host deploy hook, with no code at all.
  • Use repository_dispatch when you build in GitHub Actions or need to filter events.
  • Add a concurrency group so a burst of edits produces one build, not twenty.

Option 1: CMS webhook straight to a deploy hook

Netlify (Build hooks), Vercel (Deploy Hooks) and Cloudflare Pages (Deploy hooks) each give you a secret URL that starts a production build when it receives a POST. Create one in your project’s settings, then add it as a webhook in your CMS:

  1. In the CMS, open its webhooks settings. Sanity, Contentful, Storyblok, Strapi, Directus and Payload all have one.
  2. Paste the deploy hook URL as the target.
  3. Trigger on publish and unpublish events, not on every draft save.
  4. Publish a test entry and check that a build starts on your host.

That is the whole setup. Your Astro code does not change, because the build already fetches content from the CMS through a content loader or fetch() in frontmatter.

Limit the trigger to published content. A webhook on every autosave can start dozens of builds while an editor types.

Option 2: CMS webhook to GitHub Actions

If you deploy from GitHub Actions, have the CMS call GitHub’s repository_dispatch endpoint instead. The request needs a token, so your CMS must support custom headers (most do):

POST https://api.github.com/repos/OWNER/REPO/dispatches
Authorization: Bearer <fine-grained token with Contents: read and write>
Accept: application/vnd.github+json
Content-Type: application/json

{"event_type": "cms-publish"}

Then listen for that event in your deploy workflow:

# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches: [main]
  repository_dispatch:
    types: [cms-publish]

concurrency:
  group: production-deploy
  cancel-in-progress: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          cache: npm
      - run: npm ci
      - run: npm run build
        env:
          CMS_TOKEN: ${{ secrets.CMS_TOKEN }}
      # deploy dist/ with your host's CLI here

concurrency with cancel-in-progress: true is the debounce. If an editor publishes five entries in a minute, each new event cancels the build still running, and only the last one finishes with all five changes. Our GitHub Actions workflows for Astro guide has complete deploy steps for each host.

Scope the token to the one repository, with the smallest permissions that allow dispatch, and rotate it if it leaks.

Option 3: a small endpoint in between

Sometimes you want logic before rebuilding: only rebuild when a post (not a settings document) changes, verify a signature, or rebuild two sites. If your project already has an adapter, an Astro endpoint can sit between the CMS and the deploy hook. Verify the CMS signature first; receiving webhooks in Astro shows how. Then forward:

// src/pages/api/cms-hook.ts
import type { APIRoute } from 'astro';
import { DEPLOY_HOOK_URL } from 'astro:env/server';

export const prerender = false;

export const POST: APIRoute = async ({ request }) => {
  const body = await request.json();
  if (body?._type !== 'post') return new Response('ignored', { status: 202 });
  const res = await fetch(DEPLOY_HOOK_URL, { method: 'POST' });
  return new Response(null, { status: res.ok ? 204 : 502 });
};

Declare DEPLOY_HOOK_URL as a server secret in the env.schema of astro.config.mjs with envField.string({ context: 'server', access: 'secret' }). Check your CMS’s payload shape; _type is Sanity’s field name and other CMSs differ.

Combine with a schedule

Webhooks rebuild on change; they do not help with time-based content such as scheduled posts or expiring events. Most sites with a CMS use both: a webhook for edits and a daily scheduled rebuild for dates. Our headless CMS guide covers choosing and connecting a CMS, and any static Astro theme can take content this way.

FAQ

How do I trigger an Astro rebuild when content changes in my CMS?

Create a deploy hook in your host, then add that URL as a webhook in your CMS that fires on publish. Each publish sends a POST to the hook and starts a new build.

Why does my Astro site not show new CMS content?

Static pages fetch CMS content at build time, so new entries appear only after a rebuild. Add a CMS webhook that calls your deploy hook, or render the affected routes on demand.

How do I stop many CMS edits from starting many builds?

Trigger the webhook only on publish events, and if you build in GitHub Actions, add a concurrency group with cancel-in-progress: true so newer events replace running builds.

Is the deploy hook URL secret?

Yes. Anyone with the URL can start builds and use your build minutes. Keep it in your CMS’s webhook settings or a secret store, and regenerate it if it is exposed.