Scheduling blog posts
When starting this blog I chose to make static site with hugo and host it in netlify and use github as source control. As being static I wondered how to schedule blog posts like you can easily do in ie. Wordpress. I found this https://serverless.com/blog/static-site-post-scheduler/ blog post that tackled the problem with AWS. As mostly an Azure guy I thought, why not do it in Azure with Logic Apps
How it works
- New blog post is placed in a new branch in Github
- When post is ready to be scheduled, I create a pull request with scheduling info in pull requests message
- Github webhook fires on pull request action and invokes a Logic App that saves pull request info to blob
- Another Logic App is scheduled to run once in an hour to check if there’s pull request blobs and checks scheduling. If current timestamp is less (or equal) to timestamp in scheduled in blob, Logic app merges pull request to master branch.
- Netlify polls changes in the repository in Github and deploys changes eg. new blog posts
How to do it
First few presumptions:
- Website in Netlify is attached to repository in Github containing Hugo website “code”
- Netlify is set to automatic deploys, meaning master branch changes in Github repository triggers website deployment
Create Logic App acting to pull requests
First some sort of storage is needed to store pull requests that have schedule info included. I chose blob storage for simplicity. The Logic App could not be more simple than this: receive http request and store request body to blob.
There’s an expression in blob name that adds timestamp to blob name.
Create Logic App polling pull request blobs
Now the actual schedule handling is done in another Logic App.
Here the logic is somewhat more complex.
- We trigger Logic App to run every hour
- Next we get a blob list from storage
- Then loop through the list of blobs
- If blob doesn’t have action “opened” and “schedule” keyword in body then delete it
- Otherwise we check should we merge the pull request
- If yes then get info from blob for merging and do the merge
- When merged finally delete the blob
This way blobs that have scheduling info but schedule time is in the future, remain in storage.
The format for scheduling message in pull request commit is like this: schedule(2019-01-12T15:00:00). Logic App compares current datetime to the timestamp in scheduling message and if current datetime is greater then we do merge.
Things to do in Github
For this to work we must add some configurations in Github.
- Add webhook to github repository activated by pull requests point the webhook url to our Logic App storing pull requests to blobs. The url for webhook we of course get from our Logic App.
- Create a personal access token in Github so we can authenticate with it in our Logic App that does the merge to pull request.
Thats it, you’re done
If all went well, we now have a method to schedule blog posts with static website. Actually this blog post is scheduled with the process described here :)
Feel free to ask me more info if I left something unclear.