Adding Github Actions to this blog

I went ahead and relaxed a bit with my blog restrictions. In particular, the No JavaScript one. We like JS. However, I was not going to do the whole JSON databasing shebang, fetching, caching, etc…, like I did before.

I wanted something simpler. I'd heard about Github actions, but always wanted to try. So here's what I did.

Tinker with a workflow

I added a posts subdirectory in which I place every post I make for this blog. From that directory, I want Github Actions to pick up all posts and stack them up into the main index file, in reversed chronological order. Every time a new post is added, I need the index file to be updated. Therefore, this needs to happen on every push. It boils down to:

  1. Create a directory for your posts
  2. Create a function that updates the index file with the new posts
  3. Execute the update function on every new post

The first point speaks for itself: put new posts in their own html files and in one subdirectory. Now, for point 2, the function can be written in quite any language. I chose to do this on python, because why not delve into the rawness of that html.parser.

Anyway, after a few hours and a few classes later, the update_posts.py function was finished. It boiled down to three steps:

  1. Grab the index file and clear the section that will include all posts
  2. For every post, grab it and run a pre-formatter, and sort them in reverse chronological order
  3. Stack the posts up on the posts section of the index file

The trick here was to: identify the posts sections in the index file, wrap the individual posts in their own main tag so parsing is easy, and add a time tag on each post with the timestamp for sorting.

So, that was it with step 2. It took a while to get the parsing right, but html is such a powerful and robust way to structure text that it makes things easy; and, Python and strings do work quite nicely together.

Finally, the gist was setting up a workflow with a yaml file. Here is the gist:

name: Auto-update posts
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setting up python
        uses: actions/setup-python@v2
        with:
          python-version: 3.9.7
      - name: Updating posts
        run: |
          python update_posts.py
      - run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add .
          git commit -m 'Auto-Update posts'
          git push
      

The basic steps in the gist above are:

  1. Set up python
  2. Run update_posts.py
  3. Make and push a commit to github
And voilà, every-time a new post is added to the posts directory this action gets fired up.

So, that was it for blog systems: completely replaced by a script and a set of instructions, and of course, git and the Github Actions backend 😅