Unlocking the Potential of GitHub as a CMS

When thinking about managing content for your website, traditional CMS platforms usually come to mind. However, as static site generators become more popular and workflows shift towards simplicity and flexibility, using GitHub as a content management system (CMS) has become increasingly popular. In this post, we will explore how to use GitHub's features to efficiently manage your blog or website content.

Why Use GitHub as a CMS?

GitHub is primarily known as a platform for version control and collaboration on code, but its features can be adapted for content management. Here are some benefits of using GitHub as a CMS:

  1. Collaboration: GitHub makes it easy for multiple contributors to collaborate on content, submit changes, and review edits through pull requests, which is advantageous when working with a team.

  2. Markdown Support: GitHub supports Markdown, which is perfect for writing blog posts or documentation in a clean, readable format.

  3. No Need for Authentication: Storing your content in a public or private repository on GitHub allows you to avoid the hassle of setting up user authentication for a CMS.

Setting Up GitHub as Your CMS

To make GitHub a fully functional CMS, there are two steps required: create a bash file to create new markdown files and a GitHub workflow file to commit those changes.

  • Step 1: Create a new file at the root of the project, "newblog.sh".
#!/bin/bash

# Get current date in YYYY-MM-DD format
CURRENT_DATE=$(date +"%b %d %Y")
# Get last commit message (using Git)
LAST_COMMIT_MESSAGE=$(git log -1 --pretty=%B)
# Desired filename with dynamic date
FILENAME="src/content/blog/${CURRENT_DATE}-$LAST_COMMIT_MESSAGE.md"

# Build the frontmatter
CONTENT="---
title: '$LAST_COMMIT_MESSAGE'
pubDate: '${CURRENT_DATE}'
---
"
# Create the file with content
echo "$CONTENT" > "$FILENAME"
cat newblog.md >> "$FILENAME"

echo "Blog post file created: $FILENAME"
  • step 2: created a github workflow file to commit changes of created files .github/workflows/post.yaml

we need to give github actions a write access on your repository, to do that go to your repository setting Actions -> General, then make sure Actions permissions is set to Allow, and make sure Workflow permissions is set to Read and write permissions.

If that is grayed out, go to your organization settings Actions -> General, it will have similar settings there.

name: post new blog

on:
  pull_request:
  push:
    paths:
# if there a change on newblog.md tis workflow will run
      - 'newblog.md'

jobs:
  build:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: new blog
        run: ./newblog.sh
# commit changes
      - uses: actions4git/add-commit-push@v1

With GitHub, you can quickly and easily set up a powerful and manageable CMS in just a few steps.