Use GitHub Actions to update your profile page

If you have a GitHub account, then you can add content to you profile page with a README. If you want to find some inspiration for your own profile README then look here.

My own README is pretty boring, but I did want to display a list of my latest 5 blog posts, and of course I don’t want to maintain it manually.

A stylised graphic of the a GitHub Action workflow

GitHub Actions image by GitHub

Since the README content is stored in a GitHub repo you can use GitHub Actions to update the content whenever you see fit. For example when you publish a new blog post.

In this post I am going to assume you can setup and use GitHub actions, you know what an RSS feed is, and you aren’t frightened by words like XPath.

This a well trodden path, and if you want to do this for yourself I recommend you start with this excellent article by Simon Willison. Using his approach with a decent XML parsing library (for example feedparser in Python) is much easier to maintain and much more flexible than the approach I take.

I wanted to use XPath and GNU Sed, because a) the RSS feed on my blog is pretty simple, b) I like making life hard for myself, and c) I persuaded myself this was more efficient (which is probably true).

I’ve adapted Simon’s Actions YAML file, but made some changes to install xmllint and curl, as I’m not using Python for this. I’ve also made some updates to the git commit portion of the action as well to check for changes using a Git porcelain command.

[ -z "$(git status --porcelain)" ] && echo No Change Needed && exit 0

You can find my actions file here. Notice that it contains some very hairy sed scripting, and depends on features specific to GNU sed.

However, in case you are interested the script works as follows:

  1. Use an XPATH query extract the top 5 blog entries from my RSS feed. They are in date order so that means the five latest
  2. Use sed to create a markdown table topped and tailed by “magic markers”. The result is piped to stdout:
    1. Output the top marker
    2. Output the two lines of table header
    3. Loop through the XML entries and extract the fields of interest using regex substitution. Add in the markdown table delimiters during the substitution.
    4. Remove some extra comments in the XML we don’t need (/<!--\sraw\sHTML\somitted\s-->/s///g)
    5. Append the trailing magic marker
  3. Run sed again, this time on the README.md file. Look for the top and trailing markers, and replace them (and any content in between) with the content from step 2 which is read from stdin.
A meme showing the author ironically prefers using sed and XPath instead of an easy to use Python library

from Imgflip Meme Generator

In order for this to work of course, you need to add the magic markers manually before you run this for the 1st time.

As I said, I like making my life difficult – don’t be like me.

Finally, as my blog is hosted elsewhere, I need to make an API call (from my publication script) every time a new article is posted to trigger the GitHub workflow and get the README updated.

The API call is authenticated, so you will need create a personal access token. Be aware that access tokens expire, so create a calendar reminder for yourself to regenerate the token before that happens.

Once you have your access token safely stored (I’ve exposed below it as “GITHUB_ACCESS_TOKEN”) you can execute a GitHub actions workflow by making a POST request to the Create a workflow dispatch event endpoint. Here is an example using curl:

curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ${GITHUB_ACCESS_TOKEN}" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/alecthegeek/alecthegeek/actions/workflows/build.yml/dispatches \
  -d '{"ref":"master"}'

You can see the final result here.

comments powered by Disqus