Schedule Post on gatsby with Github Actions
6/15/2020
Introduction
You might like me have an habit of writing many post within a short span and then …
for some time (or ages :)) before you will write new post.
So a solution to the problem can be to post all the post not at once, but with some interval between, or you could write new post regularly :).
As I was publishing post to my blog through Github so I wanted something which would work with my flow.
Let’s Begin
I would plan to accomplish by using Netlify Auto Publish and scheduling merge of post to master by using Github Action.
I orginally planned to use merge-schedule-action but I wanted some more features like
- Publish those PR’s which have a specified Label.
- Exclude draft PR
So I create a fork scheduled-merge-action from the merge-schedule-action.
I wanted publish PR with specific label as draft PR are not available for private repo for free use.
Add the workflow
To use any action on Github, you have to create a workflow YAML for that action in .github/workflows
My YAML file for my workflow is as follows. You can point to the action in workflow by using {org-name}/{repo-name}@{branch}
format. For my repo’s master it will be joeljacobdev/scheduled-merge-action@master
. If you need to have some changes you can open the issue (and can make
PR if you can) or fork my repo and use it after making the required changes.
your version of action.
name: Publish scheduled Post
on:
schedule:
- cron: "0 1 * * MON"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v1
- name: merge action
uses: joeljacobdev/scheduled-merge-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LABELS: "ready"
You can use crontab get the right cron which you want to schedule for.
Also note “0 1 * * MON” here means it will run every Monday on 1 AM UTC (timezone also got me), so every PR with date less than Monday’s date will be fetched and filtering based on draft PR and only PR with label “ready” will be merge if schedule’s date is older than current date. Also for now Github scheduling only support UTC timezone, you can see the discussion here.
Now commit and push changes to your master.
Creating the PR to schedule
Makes some posts which you want to merge on schedule. Add /schedule 2020-06-14
to the PR’s body.
Add the label “ready” or whatever label you want to use for LABELS in workflow file. You can add multiple comma separated
label to LABELS.
Finally enable Auto Publishing of master or whatever your production branch is on Netlify.
And now wait for your schedule action to complete so that your post is merged to master, be careful with cron schedule, recheck it as I had got it wrong in my first try.