Logo

Armand.nz

Home / About / Linkedin / Github

Use Github API to force Github Pages rebuild site

#github |

One of the great features of GitHub Pages is that it automatically rebuilds and republishes your pages whenever you make a new commit. However, I recently ran into a situation where my new commit wasn’t being built. While I could have just made another commit to trigger the build, I wanted to find a nicer solution that would force Github Pages to build my site. After doing some research, I found out that Github provides an API for requesting a page build.

i.e., This did not prompt an immdiate update :-(

# Update blog by commiting new posts and rebuilding pages on github
git add .; git commit -m 'rebuild pages' --allow-empty; git push origin main

Here’s what did:

  1. Create a personal access token - Follow Creating a personal access token

    Log in Github account and navigate to: Setting -> Developer settings -> Personal access tokens -> Generate new token

  2. Call the Github API using a script or command line from your terminal:

  export REPONAME=armsultan.github.io
  export USERNAME=armsultan
  export TOKEN=XXXXXXXXXX

  curl -u $USERNAME:$TOKEN -X POST https://api.github.com/repos/$USERNAME/$REPONAME/pages/builds -H "Accept: application/vnd.github.mister-fantastic-preview+json"

You should see the following reponse

  {
    "status": "queued",
    "url": "https://api.github.com/repositories/XXXXXXXXXX/pages/builds/latest"
  }

Check out more details on GitHub Pages API here

comments powered byDisqus

Copyright © Armand