Stephan van Rooij

Software architect with a passion for home automation.

Daily rebuild github pages

D

Today I switched to personal website for my new personal page on Github. It’s a pretty nice template to generate a static page with the use of Jekyll, at build time it loads some details from github (like my repositories). This means that the page will only show the data of the last time it was build.

Github actions to the rescue

You can however trigger a Github Page build by calling some github endpoint. So let’s do that on a regular interval.

  1. Setup a personal access token with public_repo.
  2. Save the token as secret in the repository as GH_TOKEN.
  3. Add a workflow file to trigger the build.
# File: .github/workflows/rebuild.yml
name: Rebuild

on:
  schedule:
    - cron:  '0 6 * * *' # Runs every day at 6am

jobs:
  refresh:
    runs-on: ubuntu-latest
    steps:
      - name: Call GitHub pages build endpoint
        run: |
          curl --fail --request POST \
            --url https://api.github.com/repos/${{ github.repository }}/pages/builds \
            --header "Authorization: Bearer $GH_TOKEN"
        env:
          # Create a token at https://github.com/settings/tokens
          # since you cannot trigger a new build with the regular token.
          GH_TOKEN: ${{ secrets.GH_TOKEN }}

You’ll need an extra personal access token because you cannot trigger an action from an action, this could potentially lead to an endless loop. In all other cases you shouldn’t need to create a new personal access token, since you already get the ${{ secrets.GITHUB_TOKEN }} inside each Github actions run.

Clone a BIG git repository

C

Wanted to clone a repository to do a quick text fix and create a pull request. Created the fork, and tried to clone. Cloning the repository took way longer than I’m used to.

Slow git clone

Github Actions: Use secret file

G

Github Actions are great for automating tests and builds, among other things. If you need a secret (key/token/password), you can add those in the configuration and use them in your workflow.

Sometimes you need a file that is meant to be secret inside your workflow. This post shows you how to securely save this file as a secret and recreate the file during build. We use base64 encoding for a way to convert any file to a string that can be saved in the secrets.

This is all done in powershell core, which is available in all (Windows/Mac/Linux) runners on Github. The code below should work on any platform, but is only tested on a windows-latest runner.

Github pages (Jekyll) live reload with docker

G

I really like Github Pages to host static webpages. Static in this case means the files are generated at build time (so once when you publish a new version), instead of every time like with a wordpress website. One of the main benefits is that it results in a blazing fast website.

Like what you're seeing? Consider Sharing on Twitter or Sponsoring me