Scheduled Workflow with Cron
Run GitHub Actions workflows on a schedule using cron expressions. Covers daily builds, weekly dependency updates, nightly tests, and timezone considerations.
Advanced
Detailed Explanation
Scheduled Workflows with Cron
GitHub Actions supports schedule triggers using POSIX cron syntax. Scheduled workflows run on the default branch and are useful for recurring tasks like nightly builds, dependency updates, and periodic health checks.
Workflow YAML
name: Scheduled Tasks
on:
schedule:
- cron: '0 6 * * 1-5' # Weekdays at 6:00 AM UTC
- cron: '0 0 * * 0' # Sundays at midnight UTC
jobs:
nightly-tests:
if: github.event.schedule == '0 6 * * 1-5'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:e2e
weekly-audit:
if: github.event.schedule == '0 0 * * 0'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm audit --production
- run: npx depcheck
Cron Syntax
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * *
Common Schedules
| Expression | Description |
|---|---|
0 0 * * * |
Daily at midnight UTC |
0 */6 * * * |
Every 6 hours |
0 6 * * 1-5 |
Weekdays at 6 AM UTC |
0 0 1 * * |
First day of every month |
0 0 * * 0 |
Every Sunday at midnight |
Important Notes
- All cron times are in UTC. Adjust accordingly for your timezone.
- Scheduled workflows run on the default branch only.
- GitHub may delay scheduled workflows during periods of high load.
- The minimum interval is every 5 minutes, but GitHub discourages intervals shorter than 15 minutes.
- Use
github.event.scheduleto differentiate between multiple cron triggers in the same workflow.
Use Case
Running nightly end-to-end tests, weekly dependency audits, periodic database backups, stale issue cleanup, or any recurring maintenance task that should run automatically without manual triggers.