Scheduling posts when publishing with Gatsby and Netlify

February 20, 2020

Hey there 👋 A quick interruption before you start reading.
I've been working on a new project called StellarAdmin that helps ASP.NET Core developers like you rapidly create admin screens for your application's Admin and Support users.
If this sounds like something that will save you time on your projects, please do me a favour and check it out🙏🙏🙏

Introduction

Last year, I wrote a series of blog posts about migrating my blog to Gatsby. One of the frustrations with static website generators like Gatsby is that they have no proper way to schedule posts. When using a CMS like WordPress, you can easily schedule the publication of content on a future date.

A CMS like WordPress easily handles scheduling because each time a user views a page, it generates the content dynamically. Static generators like Gatsby, on the other hand, generate the content once when you publish it and then serves the static content for all requests.

Being able to schedule the publication of content is useful, as it allows you to batch the creation of the content. So you can, for example, sit down and in one day and produce content for the next few weeks.

Even though scheduled publishing is not a feature of static generators, in this blog post, I demonstrate how you can still achieve this.

How I currently publish content

As mentioned earlier, I use Gatsby to generate the content for my website. I host the source for the website on GitHub and have configured automated deployment to Netlify. This is a fairly common setup when using Gatsby, and you can find details on how to configure this in the Deploy to Netlify document on the Gatsby website.

I still try to batch content and publish it in the future. I create a new branch in my Git repository for every blog post and, once I complete the blog post, I push the branch to GitHub and create a pull request for it.

You can see the example of the pull request for this blog post below.

Pull request for a future blog post

At this point, the only thing left to do is to merge the pull request on the scheduled date. Currently, this is a manual process that adds unnecessary friction. If I could somehow automate this, I would have a publishing flow that is much more streamlined.

Automatically merging the pull request

When GitHub announced Actions last year, my immediate thought was that this would be a great way to schedule the publishing of content by merging a pull request on the scheduled publication date. At the time, I did not have access to the beta to try it, so over time, I completely forgot about this.

Last week I was sitting down to generate a bunch of content for my blog and started thinking about this again. The advantage of time passing since the release of Actions was that I suspected that someone else would have figured this out in the meantime and I could reuse it. Sure enough, looking through the actions on the GitHub Marketplace, I came across one called Merge Schedule.

Merge Schedule on the GitHub marketplace

According to the description, it allows you ”to merge pull requests on a scheduled day”. Precisely what I was looking for!

Creating the action

To make use of this action, go to the Actions tab of your GitHub repository and select the Set up a workflow yourself option.

The actions tab for your repository

Give the new action a name and use the following YAML for the action:

name: Merge Schedule
on:
  pull_request:
    types:
      - opened
      - edited
  schedule:
    # https://crontab.guru/every-hour
    - cron: 0 * * * *

jobs:
  merge_schedule:
    runs-on: ubuntu-latest
    steps:
      - uses: gr2m/merge-schedule-action@v1.x
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Here is a screenshot of me creating the action.

Your new GitHub action

When finished, click on the Start commit button, and go through the process of committing the action to your repository.

Scheduling the merge

Now that we have created the action, we can use it to schedule the merging of pull requests. To do that, add a line at the end of the pull request description looking like the following:

/schedule YYYY-MM-DD

YYYY-MM-DD is the date to merge the PR. For example, if I want to merge the PR for this blog post on 20 February 2020, I add the following line:

/schedule 2020-02-20

The checks at the bottom of the pull request updates and the Merge Schedule check indicates that it has scheduled the merge of the pull request.

Pull request scheduled

At this point, you can sit back and relax, knowing that the publication of your content is happening on the scheduled date without any more intervention required from your side.

Conclusion

In this blog post I demonstrated how you can use pull requests and GitHub Actions to schedule the publication of content on a future date when using technologies such as Gatsby and Netlify.

PS: If you need assistance on any of your ASP.NET Core projects, I am available for hire for freelance work.