Mahdyar's Blog

My Thoughts

How I automated aging in my GitHub profile!

Automation is what we do. We like to make things work automatically. and I did it even on my GitHub profile page. so here’s how I automated aging on my GitHub profile!

The Dilemma

I had two options:

  1. Write a script to calculate my age from my bday and replace it in the template file and then commit it to the main file.
  2. Use an environment variable that keeps the age and increase it every year.

At first, I went with the second one, and there were some problems. for example, if I wanted to run the job manually and see what happens, it would increase the age, or it should’ve updated the workflow as well, but it wasn’t possible by GitHub API and PyGitHub without two commits.

So as you’ve probably guessed, I went with the first one.

The Workflow

GitHub Actions were enough for me, and because of that, I didn’t need to use a third-party app for automation.

For my aging script, I needed it to be run at a particular time, so I used scheduled events:

on:
  schedule:
    - cron: "00 12 08 01 *" # My Bday!

and it’ll be run at 12:00 UTC on the 8th in January of every year.

The Script

For committing to GitHub, you’ll need a GitHub Access Token that can be generated from here. After generating one, you’ll need to add it as a secret to your repository, you can read more about it here.

Now, you can access your secret by defining an environmental variable into your workflow file:

env:
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}

And now we can use it in our code:

ACCESS_TOKEN = os.environ['ACCESS_TOKEN']

GitHub has some default environment variables that can be accessed through a workflow, we here use “GITHUB_REPOSITORY”:

GITHUB_REPOSITORY = os.environ["GITHUB_REPOSITORY"]

As its documentation says, it returns:

The owner and repository name. For example, octocat/Hello-World.

so we have to extract the repository name:

repositoryName = GITHUB_REPOSITORY.split("/")[1]

And after that, there’s nothing to be explained. The script can be read from here. That’s How I automated aging.

Have fun!