Go Automate Your GitHub Profile README

Create a dynamic Markdown file with Go and GitHub Actions

Victoria Drake
Aug 5 · 3 min read
Image for post
Image for post
Illustration by author

GitHub’s new profile page README feature is having the wonderful effect of bringing some personality to the MySpace pages of the developer internet. Though Markdown lends itself best to standard static text content, that’s not stopping creative folks from working to create a next-level README. You can include GIFs and images to add some motion and pizazz (they’re covered in GitHub Flavor Markdown), but I’m thinking of something a little more dynamic.

At front and center on your GitHub profile, your README is a great opportunity to let folks know what you’re about and what you find important and to showcase some highlights of your work. You might like to show off your latest repositories, tweet, or blog post. Keeping it up to date doesn’t have to be a pain, either, thanks to continuous delivery tools like GitHub Actions.

My current README refreshes itself daily with a link to my latest blog post. Here’s how I’m creating a self-updating README.md with Go and GitHub actions.


Reading and Writing Files With Go

I’ve been writing a lot of Python lately, but for some things, I really like using Go. You could say it’s my go-to language for just-for-func projects. Sorry. Couldn’t stop myself.

To create my README.md, I’m going to get some static content from an existing file, mash it together with some new dynamic content that we’ll generate with Go, then bake the whole thing at 400 degrees until something awesome comes out.

Here’s how we read in a file called static.md and put it in string form:

The possibilities for your dynamic content are only limited by your imagination! Here, I’ll use the github.com/mmcdole/gofeed package to read the RSS feed from my blog and get the newest post.

To join these bits together and produce stringy goodness, we use fmt.Sprintf() to create a formatted string.

// Whisk together static and dynamic content until stiff peaks form
blog := "Read my latest blog post: **[" + rssItem.Title + "](" + rssItem.Link + ")**"
data := fmt.Sprintf("%s\n%s\n", stringyContent, blog)

Then, to create a new file from this mix, we use os.Create(). There are more things to know about deferring file.Close(), but we don’t need to get into those details here. We’ll add file.Sync() to ensure our README gets written.

View the full code here in my README repository.

Mmmm, doesn’t that smell good? Let’s make this happen on the daily with a GitHub Action.


Running Your Go Program on a Schedule With Actions

You can create a GitHub Action workflow that triggers both on a push to your master branch and on a daily schedule. Here’s a slice of the .github/workflows/update.yaml that defines this:

on:
push:
branches:
- master
schedule:
- cron: '0 11 * * *'

To run the Go program that rebuilds our README, we first need a copy of our files. We use actions/checkout for that:

steps:
- name: 🍽️ Get working copy
uses: actions/checkout@master
with:
fetch-depth: 1

This step runs our Go program:

- name: 🍳 Shake & bake README
run: |
cd ${GITHUB_WORKSPACE}/update/
go run main.go

Finally, we push the updated files back to our repository. Learn more about the variables shown at “Using variables and secrets in a workflow.”

View the full code for this Action workflow here in my README repository.


Go Forth and Auto-Update Your README

Congratulations and welcome to the cool kids’ club! You now know how to build an auto-updating GitHub profile README. You may now go forth and add all sorts of neat dynamic elements to your page — just go easy on the GIFs, okay?

Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Victoria Drake

Written by

Director of Engineering. I 💜 cybersecurity and developing teams. Core maintainer, OWASP Web Security Testing Guide. 👉 https://victoria.dev

Better Programming

Advice for programmers.

Victoria Drake

Written by

Director of Engineering. I 💜 cybersecurity and developing teams. Core maintainer, OWASP Web Security Testing Guide. 👉 https://victoria.dev

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store