How to force GitHub Pages build?

If you want a quick script solution, here it is. Just do the following tasks only once, and run the script whenever you want to rebuild your GitHub page.

1. Create a personal access token for the command line:

  • Follow the official help here to create a personal access token. Basically, you have to log in your GitHub account and go to: Settings > Developer settings > Personal access tokens > Generate new token.
  • Tick repo scope.
  • Copy the token.

2. Create the following script:

  • Create a file called RebuildPage.sh and add the lines:

    #!/bin/bash
    curl -u yourname:yourtoken -X POST https://api.github.com/repos/yourname/yourrepo/pages/builds
    

    Here,

    • Replace yourname with your GitHub username.
    • Replace yourtoken with your copied personal access token.
    • Replace yourrepo with your repository name.

3. Run the script:

  • If you use Windows 10:

    • You need to setup Windows Subsystem for Linux, if not already done. Follow this to do so.
    • Remove the first line (#!/bin/bash) from the script and save the script as RebuildPage.bat. (i.e., replace .sh with .bat in the script file name)
    • Alternative to the above point: To get the double-click feature for running the .sh file:

      • Set bash.exe as the default program for .sh files.
      • Open regedit.exe and edit HKEY_CLASSES_ROOT\Applications\bash.exe\shell\open\command. Set the (Default) value to:

        "C:\Windows\System32\bash.exe" -c " \"./$(grep -oE '[^\\]+$' <<< '%L')\";"
        
    • Now double-click the script wheneven you want to rebuild your GitHub page. Done!

  • If you use Linux/Mac, running the script is as same as running other scripts. Done!

Additional notes for the solution:

This solution utilizes a API of GitHub REST API v3. Here is the official documentation for the API.


From GitHub support, 2014-06-07:

It's not currently possible to manually trigger a rebuild, without pushing a commit to the appropriate branch.


Edit:

As Andy pointed out in the comments, you can push an empty commit with the command:

git commit -m 'rebuild pages' --allow-empty
git push origin <branch-name>

Edit 2:

Thanks to GitHub Actions, it's fairly easy to trigger a daily publish: https://stackoverflow.com/a/61706020/4548500.

Tags:

Github Pages