Ship & operate · Lesson 11 of 15 · 15 min

Deploy, and rebuild on publish

Put the site on Vercel, then add a webhook so every publish in Contentful starts a fresh build.

1. Push to GitHub and import into Vercel

git add .
git commit -m "Learn Hub with Contentful"
gh repo create learn-hub --private --source=. --push

No GitHub CLI? Create an empty repo on github.com/new and push to it, the same way as in the Astro deploy lesson.

Then go to vercel.com/new, import learn-hub, and stop before you click Deploy.

2. Give Vercel your keys

Your .env file isn’t in git, so Vercel doesn’t have it. Open Environment Variables on the import screen (or Settings → Environment Variables later) and add:

Name Value
CONTENTFUL_SPACE_ID Your Space ID
CONTENTFUL_DELIVERY_TOKEN Your Delivery token

Leave the Preview token out for now. Production should never be able to read drafts.

Click Deploy. The build log shows your three loaders, and you get a live URL.

3. Create a deploy hook

Right now a new lesson only appears after the next git push. Fix that with a deploy hook: a secret URL that starts a Vercel build when anything sends a request to it.

  1. In Vercel, go to Settings → Git → Deploy Hooks
  2. Name it contentful, branch main, and click Create Hook
  3. Copy the URL

Test it:

curl -X POST "YOUR_DEPLOY_HOOK_URL"

A new deployment appears in Vercel.

4. Add the webhook in Contentful

A webhook is Contentful calling a URL when something happens.

  1. In Contentful, go to Settings → Webhooks and click Add Webhook
  2. Name: Vercel production
  3. URL: choose POST and paste the deploy hook URL
  4. Triggers: choose Select specific triggering events. In the Entry row and the Asset row, tick Publish and Unpublish. Leave everything else empty.
  5. Filters: keep the filter that limits it to the master environment
  6. Save

5. Watch it work

Edit a published lesson’s title in Contentful and click Publish. Open Vercel. A deployment starts within seconds. About a minute later the live site shows the new title.

Back in Contentful, open the webhook and look at its Activity log. Every call is listed with the response it got. This is the first place to look when a publish doesn’t show up.

The full loop

Editor clicks Publish
        │
        ▼
Contentful webhook ──POST──▶ Vercel deploy hook
                                    │
                                    ▼
                         astro build runs the loaders
                                    │
                                    ▼
                         Delivery API returns published entries
                                    │
                                    ▼
                         Static HTML goes live on the CDN

Why only Publish and Unpublish

Contentful autosaves while an editor types. If the webhook fired on every save, one paragraph could start twenty builds. Publish and Unpublish are the only events that change what the Delivery API returns, so they’re the only ones production cares about.