How I use Github Actions to schedule Gatsby blog posts
Nick Scialli
May 19, 2021
Gatsby is great, but it doesn’t provide much (or rather, anything) in the way of scheduling content publication. Today, I’ll show you how I schedule my Gatsby blog posts using Github Actions.
My Workflow
I develop a new blog post on its own branch locally and keep it up-to-date with an upstream branch on my blog’s github.com repository. When I’m done writing, I create a PR from that branch into main
. Finally, I add the following description to the PR, which will tell our soon-to-be-developed action when to merge the branch:
/schedule 2021-05-04
This note tells the action to merge the branch into main
on May 4, 2021. My Gatsby blog deploys to Netlify through a Github integration, so as soon as this new branch is merged to main
, a new build is initiated and deployed!
The Action
To add this action to your app, create a /.github/workflows
directory at the root of your project. In that directory, create a file called merge-schedule.yml
with the following contents:
/.github/workflows/merge-schedule.yml
name: Merge Schedule
on:
pull_request:
types:
- opened
- edited
- synchronize
schedule:
- cron: 0 * * * *
jobs:
merge_schedule:
runs-on: ubuntu-latest
steps:
- uses: gr2m/merge-schedule-action@v1
with:
merge_method: squash
time_zone: 'America/New_York'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Note that this workflow is powered by the excellent merge-schedule-action action—you will probably even notice my yaml file is strikingly similar to the example they give!
One question you might have on this file is that it looks like a GITHUB_TOKEN
secret is required. There’s actually no need to worry about this secret, it’s automatically provided in the Github Actions environment.
Customize Your Configuration
You may want to customize this yaml file to your own liking. As-is, it runs on an hourly schedule, but you may choose to only release posts at a certain time of day. You can adjust the schedule by changing the cron
property. If you’re not familiar with cron job sytax, take a peek at crontab guru!
Also note that the time_zone
property is set for "America/New_York"
; you may want to adjust this for your own time zone.
The Downside: Redundant Date Specification
The one downside to how I schedule/release my posts is that I end up specifying the release date in three different places.
- First, I put it in the
frontmatter
field for my blog post since that’s the only user-facing indication of the publication date. - Next, I include the date in the branch name for my post. For example, the post you’re reading right now is being developed on a branch called
2021-05-19-scheduling-gatsby-posts
. Note that this isn’t a required part of the workflow, it just helps me easily tell the dates on which I have scheduled posts. - Finally, as discussed before, I add the scheduling date in the description of the PR (e.g.,
/schedule 2021-05-19
).
It’s not that big a deal to re-write the date three times, but it does feel slightly inefficient and leaves room for error.
Conclusion
I hope this helped you figure out how to schedule your Gatsby posts!
Nick Scialli is a senior UI engineer at Microsoft.