Sub topic 8
Running on a schedule with cron
This chapter assumes you can read YAML and have written a basic workflow with an on key. You should also know what a branch and a default branch are. Here you will add a schedule trigger and learn to read its cron expression.
How the schedule event works
The schedule event starts a workflow at set times, with no push or pull request involved. It is useful for nightly builds, weekly dependency checks, and cleanup jobs.
You list one or more cron entries under schedule. Each entry holds a cron expression, which is a string of five fields separated by spaces.
on:
schedule:
- cron: '30 5 * * 1-5'This workflow runs at 05:30 UTC, Monday through Friday.
Reading the five fields
The fields use the POSIX cron format. From left to right they are:
| Field | Allowed values | Example meaning |
|---|---|---|
| Minute | 0-59 | `30` is minute 30 |
| Hour | 0-23 | `5` is 05:00 |
| Day of month | 1-31 | `*` is every day |
| Month | 1-12 or JAN-DEC | `*` is every month |
| Day of week | 0-6 or SUN-SAT | `1-5` is Monday to Friday |
Four operators are available:
*matches any value.,lists values, such as1,15.-gives a range, such as1-5./gives a step, such as*/15for every 15 minutes.
Timing behavior to expect
Scheduled runs are not exact. Keep these points in mind:
- Time zone. Cron times are always UTC. You cannot set another zone in the
cronstring, so convert local times yourself. - Delays. Runs can start late, especially at the top of the hour when many workflows fire together. Picking an off-peak minute such as
17instead of0reduces the delay. Under heavy load, a run may occasionally be dropped. - Shortest interval. The minimum interval is once every 5 minutes.
- Default branch only. The workflow file that counts is the one on the default branch. A schedule defined on another branch will not run.
- Inactivity. In a public repository, GitHub disables scheduled workflows after 60 days without repository activity. You can re-enable them in the Actions tab.
Several schedules in one workflow
You can list more than one cron entry. When you need different behavior for each, read the github.event.schedule value, which holds the expression that fired.
name: Scheduled checks
on:
schedule:
- cron: '17 6 * * 1-5'
- cron: '43 2 * * 0'
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Show which schedule fired
run: echo "Schedule: ${{ github.event.schedule }}"
- name: Sunday-only step
if: github.event.schedule == '43 2 * * 0'
run: echo "Running the weekly deep check"Common errors
- Expecting a run right after merging the workflow. The first run happens at the next matching time.
- Writing local times and getting runs hours off. Convert to UTC.
- Using a feature branch to test. Schedules only run from the default branch, so add
workflow_dispatchfor manual testing.
Exercise
Write a cron expression that runs at 18:45 UTC every Sunday. Then say which branch the workflow file must be on for it to run.
Show answer
45 18 * * 0 (or 45 18 * * SUN). The file must be on the repository's default branch.
Exercise
A schedule set to 0 * * * * in a public repository stopped running after two months of no commits. What is the likely cause, and how do you fix it?
Show answer
GitHub disables scheduled workflows in public repositories after 60 days without repository activity. Re-enable the workflow from the Actions tab, or make new repository activity.