Building a Git-to-Contentful Blog Draft Pipeline with GitHub Actions
Write in Markdown, commit local diagrams, and let GitHub Actions sync unpublished drafts to Contentful—then auto-archive once published.

I love Contentful for hosting structured blog content, webhooks, and image optimization—but when I'm actually writing a technical post, I'd much rather write in Markdown inside my editor (or pair with an AI coding agent) than click around a web form. ✍️
At the same time, I still want Contentful Studio to be the final gate before anything goes live: previewing the layout, attaching a featured image, and clicking Publish myself.
So I wired up a lightweight Git-to-Contentful pipeline using GitHub Actions and contentful-management that gives me the best of both worlds.
📁 1. Organizing Every Post in Its Own Folder
Each post lives in a self-contained folder named after its slug:
drafts/<slug>/index.mdholds the YAML frontmatter (title,subtitle,slug,excerpt,publishDate,tags,author) and Markdown body.drafts/<slug>/images/holds any local diagrams or screenshots referenced with standard Markdown ().
Keeping the Markdown and its images side-by-side means nothing gets orphaned in a giant global images folder—and when a post is published, moving the entire folder to published/<slug>/ keeps everything neatly grouped.

⚙️ 2. Markdown to Contentful Rich Text (With Automatic Image Uploads)
The core script, scripts/create-contentful-draft.js, parses the YAML frontmatter and converts headings (##, ###, ####), lists, blockquotes, horizontal rules, bold/italic/code marks, and hyperlinks into a valid Contentful Rich Text Document.
Two small details made a huge difference in day-to-day use:
Code-styled clickable links: Wrapping a link label in backticks keeps inline
CODEformatting inside theINLINES.HYPERLINKnode (even inside bold text or paths containing[slug]), andRichTextRenderer.tsxopens every link in a new tab.Automatic local image uploads without duplicates: Whenever the parser sees a local Markdown image (
), it checks Contentful Media for an existing asset named<slug>--<filename>. If it's already there, it reuses it; otherwise it uploads, processes, and publishes the image asset and inserts a realembedded-asset-blocknode into the Rich Text body. 🖼️
🛡️ 3. Safe Upserts, Never Auto-Publishing
Running an automation script twice should never create duplicate blog posts or accidentally push half-finished edits live.
Here's how scripts/create-contentful-draft.js stays idempotent and safe:
Upsert by
slug: It queries Contentful for an existingblogPostmatchingfields.slug. If none exists, it creates a new unpublished draft; if one exists, it merges fields in place (preserving Contentful-only fields likefeaturedImage).Skip already-published entries by default: If
sys.publishedVersionis set, the script skips the entry unless--forceis passed—so routine runs never clobber edits made directly in Contentful Studio.Never calls
entry.publish(): Even with--force, updates land in Contentful's Changed draft state so you can inspect them in Contentful Preview before clicking Publish.
📦 4. Auto-Syncing Edits & Archiving Published Posts
The GitHub Actions workflow in .github/workflows/push-contentful-draft.yml ties the lifecycle together:
Push to `drafts/
onmain**: Detects only added or modified files viagit diff(even if you only updated an image insideimages/) and upserts the matchingindex.md` as a draft.Push to `published/
onmain**: Syncs edits with--force` so updates to already-published posts stage cleanly in Contentful Preview as Changed drafts.Auto-archiving via
scripts/archive-published-drafts.js: Scansdrafts/, checks which slugs are now published in Contentful, moves the entiredrafts/<slug>/folder topublished/<slug>/, and commits the move with[skip ci]so it doesn't trigger an unnecessary rebuild.
✨ Wrapping Up
Now my writing loop is dead simple: write Markdown + drop in a diagram → merge a PR → preview in Contentful Studio → click Publish. Everything stays version-controlled in Git without giving up Contentful's editorial UI.
You can inspect the full scripts and workflow in the public repo at github.com/reggiepangilinan/mywebsite.
Related Posts
Running a Full Next.js Blog on Free Tiers: A Contentful + Netlify Case Study
A data-backed case study breaking down Contentful Free, Netlify Free, and GitHub Free quotas (at the time of this writing), with real math on how many visitors, blog posts, and images a personal site can serve before ever paying a cent for infrastructure.
Swiss Design Meets Punk Rock: How I Built an Automated Hero Poster System for My Blog
A plain-English look at the HTML/SVG poster generator behind my blog hero images, a quick intro to the Swiss International Style and punk-zine energy that inspired it, and how every poster flows from Git to Contentful.
Adding AI-Powered Blog Search with Google Gemini in Next.js
How we added an intelligent, Gemini-powered search bar to a Next.js + Contentful blog—covering AI basics for non-specialists, why hybrid search beat Google Search Grounding and Vertex AI Search, real-world free-tier traffic estimates, and built-in cost guardrails.