Things I Learned #18: GitHub Actions on a Schedule
This blog is built in Jekyll, a great static site generator. Everything is in a private GitHub repository with a GitHub Action to build and deploy the site to an Azure Static Website. It all works well, and I plan to write a more detailed blog post about the move from WordPress, but for now, I want to dig more into how it's deployed.
My usual process for this blog is to work out of a branch, then create a PR. For this post, the branch is named "til18." When the post is ready to be published, I do a quick review of the PR, and then when I'm prepared to publish it, I complete the PR, which kicks off my standard pipeline.
Because this is a static site, publishing a new post means rebuilding the entire site and redeploying it. Generally, that's not a big deal, but there are times I want to schedule when a post shows up or more to the point, I want to push a post to git, then make sure it's built when I want, but not necessarily manually, and I don't want it to show up immediately.
The solution for this is pretty easy, and I actually had it in place a couple months ago, but I temporarily disabled it until I fixed some other pipeline issues.
I learned you can run a GitHub Action on a schedule. It's as simple as this:
So, instead of telling GitHub to run the Action on completing a PR or committing to a specific branch, I can give it a cron expression, and the Action will run on that schedule. Kinda cool, eh? While I want to avoid getting into the details of Jekyll and dates right now, I can push a post with a future date anytime I want before that date, complete the PR, and then when the scheduled Action runs on or after the date of the post, the post will be published.
The cron expression I included above will run the Action every morning at 2am UTC. Also, scheduled tasks like this will only run against your default, in my case, main branch. I'll need to complete my PR before the subsequent planned execution of the Action if I want the post to go live.
Want help building your cron expression? Check out https://crontab.cronhub.io/.