Introduction
You will be able to use the schedule event in GitHub Actions to trigger the execution of your workflows at selected times based on the cron schedule that you will define. In fact, that could help with automating things to happen on a regular schedule without any action on your end.
Also remember that workflows are bound to the default branch. So, if you're scheduling something, make sure it's on the right branch. If for some reason a scheduled workflow isn't running anymore, that could be either because the repository has become inactive, or else the last person who edited the cron schedule has been removed from the repo. A quick fix for this would be to have someone with access update the cron settings.
Steps we will cover in this article:
How Cron Syntax Works
Cron uses a series of five values to define time:
- Minute (0–59)
- Hour (0–23)
- Day of the month (1–31)
- Month (1–12)
- Day of the week (0–6, where Sunday is 0)
Here’s an example:
on:
schedule:
- cron: '30 5 * * *' # the workflow runs every day at 5:30 UTC.
You can utilize all manner of neat tools that help generate your cron syntax and confirm what time it will run. Here's a decent one: crontab guru, and here are some crontab guru examples to help you get started with your first few entries.
Multiple Schedule Triggers
You could have multiple schedule triggers for one workflow. This further opens a whole field of flexibility: every different kind of time could be scheduled on different days. For example:
on:
schedule:
- cron: '30 5 * * 1,3' # runs workflow every Monday and Wednesday at 5:30 UTC.
- cron: '30 5 * * 2,4' # runs workflow every Tuesday and Thursday at 5:30 UTC.
# note: skipping certain tasks on Mondays and Wednesdays.
Managing Workflow Delays
One thing to remember is that this workflow will be delayed when the system has loads, especially at the start of an hour. The best times to schedule such workflows are 15 minutes and 45 minutes when it's odd. Also, you can trigger a workflow multiple times a day using cron expressions.
Important Notes:
- Once the last user who has edited an already scheduled workflow gets removed from the repository, the latter automatically becomes disabled. In order for this user to reenable again, a person needs to change the cron schedule with write permissions.
- Scheduling workflows Automatically disables workflows that scheduled in public repositories if there's no activity in the repository for 60 days
Conclusion
Scheduling workflows properly in GitHub Actions will go a long way towards bringing efficiency and automation to your CI/CD. Using cron syntax allows you to run a workflow at the most opportune time so that tasks will be executed regularly without human intervention.