Using Github Actions to Create Jigsaw Blog Posts

June 12, 2023

I've been using Jigsaw to build this blog for the past few weeks. Static site generators have the advantage of being fast and secure, but they also have the disadvantage of being a bit more difficult to work with. One thing that is a bit of a pain is creating new blog posts. I have to create a new markdown file, create a new branch, push it all to GitHub and create the Pull Request manually.

I decided to use GitHub Actions to automate this process. GHA provides a workflow_dispatch event that allows you to trigger a workflow manually. You can also add inputs to the workflow_dispatch event. I added two inputs to my workflow: title and date. The title input is used for the post title as well as to create the slug for the new markdown file. The date input is used to set the published date. This is a simple example, but you could easily add tags, description, cover_image url, etc.

Workflow Dispatch Inputs

Once you submit, it kicks off a new action workflow that performs the following steps:

Workflow Steps

Once it finishes, you'll have a new branch with a new markdown file and a new Pull Request.

Pull Request

Below is the full workflow file. Hopefully this helps automate some of the pain away from creating posts.

name: Create Post
on:
  workflow_dispatch:
    inputs:
      title:
        description: 'Title of the post'
        required: true
      date:
        description: 'Date string'
        required: true

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

permissions:
  contents: write
  pull-requests: write

jobs:
  create_post_and_pr:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Slugify Branch Name
        id: slugify
        uses: actions/github-script@v4
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const title = '${{ github.event.inputs.title }}';
            const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '');
            console.log("::set-output name=slug::" + slug)

      - name: Create Markdown File
        run: |
          mkdir -p ./source/_posts
          echo "---" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "extends: _layouts.post" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "section: content" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "title: ${{ github.event.inputs.title }}" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "date: ${{ github.event.inputs.date }}" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "tags: []" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "description: " >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "cover_image: " >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md

          echo "---" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "" >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md
          echo "This is a new post." >> ./source/_posts/${{ steps.slugify.outputs.slug }}.md

      - name: Create New Branch
        run: |
          git checkout -b ${{ steps.slugify.outputs.slug }}

      - name: Commit and Push Changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add .
          git commit -m "Create new markdown file"
          git push origin ${{ steps.slugify.outputs.slug }}

      - name: Create Pull Request
        run: |
          gh pr create --base master --fill --title "${{ github.event.inputs.title }}"

Monitor your DNS Zones with ZoneWatcher

Be alerted of DNS record changes moments after they happen, not from upset customers.

ZoneWatcher screenshot