Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greghaskins/62e30796021e02869240 to your computer and use it in GitHub Desktop.
Save greghaskins/62e30796021e02869240 to your computer and use it in GitHub Desktop.

You can trigger a GitHub Pages (Jekyll) rebuild with a single API call. This is pretty useful for auto-publishing blog posts from a bot like Zapier in conjunction with future: false in your Jekyll config.yml. Just future-date your posts, and they'll go live when that date rolls around. I use a version of this setup for my blog at greghaskins.com.

Setup

  1. Create a GitHub personal access token and save it somewhere. It needs to have the repo access scope (at least).

  2. Create a file at the root of your repo (e.g. .publish) with some dummy content.

    $ echo ".publish" > .publish
  3. Commit it and push.

    $ git add .publish
    $ git commit -m "add .publish file for scripted publishing"
    ...
    $ git push
  4. Get the blob SHA and base64 content for it via the GitHub API

    $ curl -H "Authorization: token $YOUR_TOKEN" \
    > https://api.github.com/repos/:owner/:repo/contents/.publish
    {
      "name": ".publish",
      "path": ".publish",
      "sha": "3f949857e8ed4cb106f9744e40b638a7aabf647f",
      ...
      "type": "file",
      "content": "LnB1Ymxpc2gK\n",
      "encoding": "base64",
      ...
    }

In this case it's 3f949857e8ed4cb106f9744e40b638a7aabf647f and LnB1Ymxpc2gK (strip the trailing \n).

And you're all set.

Triggering a rebuild/publish

Just "update" the content for that .publish file with the same content as before. This creates a new commit on the GitHub side and triggers a Pages rebuild.

$ curl -H "Authorization: token $YOUR_TOKEN" \
> -H "Content-Type: application/json; charset=UTF-8" \
> -X PUT \
> -d '{"message": "re-publish GitHub Pages",
>   "committer": {
>     "name": "PublishBot",
>     "email": "[email protected]"
>   },
>   "content": "LnB1Ymxpc2gK",
>   "sha": "3f949857e8ed4cb106f9744e40b638a7aabf647f"}' \
>   https://api.github.com/repos/:owner/:repo/contents/.publish

{
  "content": {
    "name": ".publish",
    "path": ".publish",
    "sha": "3f949857e8ed4cb106f9744e40b638a7aabf647f",
    ...
  },
  "commit": {
    "sha": "57af635acad5802c0f5a166f431e4ad19db1e80f",
    "message": "re-publish GitHub Pages",
    ...
  }
}

You'll notice that the SHA before and after is the same. That means you can simply replay this same request any time you want to trigger a rebuild.

@alxmjo
Copy link

alxmjo commented May 19, 2017

Does this method still work?

@Dids
Copy link

Dids commented Feb 27, 2019

I can verify that this still works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment