Alexito's World

A world of coding 💻, by Alejandro Martinez

GitHub Action to create new posts

Now that I have this site building on Netlify I wanted to start writing more far away from my computer. Of course I immediatly found a bit more friction that I had to solve.

Posts here are just Markdown files, but they do contain a bit of metadata in form of front matter and they must be in a directory with a specific naming convention since I use a bundle format to have the images and the text side by side.

Since I have a custom CLI tool to build the blog, instead of relying on the default one provided by the static site generator Publish, it's very easy for me to add automatoin to any process. And that's what I had for a long time:

▶ amp help new
OVERVIEW: Creates a new post

USAGE: cli new <title> [--date <date>] [--disable-auto-open]


  --date <date>           (default: 2022-12-20)
  -h, --help              Show help information.

So the only thing I had to do is setup something that triggers that command with some given inputs. I could have build some website that triggered some backend API but there is an easy solution for something so simple: GitHub Actions.

The action ended up looking like this:

name: "Create new post"
run-name: "Create new post: ${{ inputs.title }}"

          description: 'Post title'
          required: true
          type: string

    runs-on: ubuntu-latest
      - uses: actions/checkout@v3
      - name: Setup Swift
        uses: swift-actions/setup-swift@v1.20.0
          swift-version: "5.7.1"
      - name: Cache
        id: cache-alejandromp
        uses: actions/cache@v3
          path: .build
          key: ${{ runner.os }}-cachev1-${{ hashFiles('**/Package.resolved') }}
      - run: swift run alejandromp new "${{ inputs.title }}" --disable-auto-open

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
          commit-message: "New post ${{ inputs.title }}"
          title: "New post ${{ inputs.title }}"

Since I'm just creating files I don't need any macOS specific API so I can run Swift on Ubuntu. I then use the workflow_dispatch in order to be able to trigger it manually from GitHub's UI. I set it up so it asks me for the title so everything is preconfigured by the action itself.

The action is quite simple, it instllas the correct version of Swift, caches the build folder, runs the command passing the given title and finally creates a PR with the changes.

Now I can click a button on GitHub, have a new post draft created and start writing from

PR example

If you liked this article please consider supporting me