Erik Yuzwa

Fullstack web developer with a touch of PC game development for good measure

7 August 2021

Schedule your Jekyll posts on Github Pages with Github Actions

by erikyuzwa

I recently migrated my website content away from a traditional web server host to Github Pages.

A key feature that initially inspired me to make this migration in the first place, is to figure out how to make use of Github automated functionality to schedule future post content.

Instead of needing to remind myself to manually rebuild my site and FTP it up to my host when needed, I wanted a way of writing a few articles on the weekend and letting them “self publish” on a specific future date.

In Content Management Systems like WordPress, it’s a trivial task to set your posts publication date to some future time at which point you have the confidence in the post being made publically available on that set future time.

Let’s sit down and achieve the same functionality (and confidence) with Github Pages!

The Gameplan

While I’m not after any kind of granular levels type of publication, I’m completely “ok” with the idea of a scheduled post being made available sometime during a 24-hour period.

There’s a few small things we’re after:

Create an Access Token

That covers “step 1”. This token is required, as our automated Github Action will be making use of it when running a job for you.

Create a “Secret” for your new Access Token

Configuring Jekyll

Now let’s update your site’s _config.yml with some entries:

future: false
timezone: your_timezone (eg. America/Dawson)

Configuring a Jekyll Post

Once we’ve updated the main site configuration, we can go into the frontmatter of any blog post / content that you want to publish sometime in the future.

Simply update the date field within the blog posts frontmatter up at the top of the file:

date: 2022-08-07 14:25:52

So as it currently stands, from now on whenever we push some content up to our Github Page repository, posts tagged with a future datetime stamp will not be published.

The final piece: adding a Github Action

Let’s take a look at this new .yml file:

name: scheduling post action
  # Triggers the workflow on a scheduled event
    - cron: '15 6 * * *' # 6:15 AM every day.
    # cron: '15 6 * * 1,5,6' will schedule 06:15 every Monday, Friday and Saturday
    runs-on: ubuntu-latest
    - name: Trigger GitHub pages rebuild
      uses: wei/curl@master
        args: "-X POST -H 'Authorization: token ${{ secrets.USER_TOKEN }}' -H 'Accept: application/vnd.github.ant-man-preview+json'${{ github.repository }}/pages/builds"
        # You must create a personal token with repo access as GitHub does
        # not yet support server-to-server page builds.
        USER_TOKEN: ${{ secrets.USER_TOKEN }}

The jobs section is the “meat” of this workflow. Once our cron hits our specific time, this workflow will activate and run whatever is defined in jobs.

In our case, we want to make use of the curl Action to send a POST request at the endpoint which will trigger a build of the Jekyll site.

Due to security access, we need to specify an Authorization header, which is created with help from the USER_TOKEN secret that we created earlier on!

I hope you got something out of this article, and it demonstrates some simple techniques that GitHub Actions can provide for your site!