EDDYMENS

Published 3 months ago

Periodically Run GitHub Actions: A Guide To Scheduled Workflows

Table of contents

Backstory

This blog is completely statically generated. You may have noticed the publishing date right above the blog title that says Published x weeks|month|year ago.

I used to generate that bit using JavaScript, because, you know, time doesn't stand still. Without it, the date would be wrong most of the time since I only regenerate the blog assets [→] when I publish something new, and it's the only time the dates are updated.

The blog also has JavaScript for the dark mode switcher and a simple script I wrote to preload links referenced in blog posts.

Anywho, of late I have been yanking out some of these scripts to keep the blog as slim as possible. For the date bit, it hit me: I already created a CI pipeline to generate my static assets whenever I push to the blog's repository using GitHub Actions [↗].

What if I could schedule the CI task to run each day regardless of whether I publish or not? This would update the dates and keep them fresh by at least a day.

I quickly checked if GitHub Actions allowed tasks to be scheduled on its free tier and suuuuure enough, it does! [↗]

So in this article, I will walk you through the steps of setting one up.

Implementation

Single schedule

Single Schedule

01: name: Deploy to Netlify 02: 03: on: 04: schedule: 05: - cron: '15 1 * * *' 06: push: 07: branches: 08: - master 09: 10: jobs: 11: build: 12: runs-on: ubuntu-latest 13: ...

Add the schedule event trigger as shown on line 04 to 05 [→] above, then set the duration (?POSIX cron generator) [↗]. On your next push, the task will be scheduled to run based on the set date/duration.

Multiple schedules

You can also set up multiple crons if you need to:

Multiple Schedules

01: name: Deploy to Netlify 02: 03: on: 04: schedule: 05: - cron: '15 1 * * *' 06: - cron: '30 1 * * *' 07: push: 08: branches: 09: - master 10: 11: jobs: 12: build: 13: runs-on: ubuntu-latest 14: ...

Note: The minimum interval you can set is to have your task run every 5 minutes. To deactivate a cron, remove it from your GitHub Action config file and commit and push the changes. This will stop GitHub Actions from scheduling future runs as well as cancel scheduled tasks.

Conclusion

So there you have it, scheduling your GitHub workflows. GitHub Scheduled tasks got me thinking about making a few tweaks to my blog so that I can have blog posts scheduled to go out on specific dates.

Here is another article you might like 😊 How To Run Laravel Queues On Shared Hosting