GitHub Actions cron 定时任务
GitHub Actions 在 on.schedule 里使用 POSIX cron,在默认分支上运行;除非条目设置了时区,否则按 UTC。 选择一个调度,查看精确表达式、可直接粘贴的配置和接下来的运行时间。
调度
- 每 5 分钟
*/5 * * * * - 每小时
0 * * * * - 每分钟无法转换
- 每 10 分钟
*/10 * * * * - 每 15 分钟
*/15 * * * * - 每 30 分钟
*/30 * * * * - 每天零点
0 0 * * * - 每天早上 9 点
0 9 * * * - 工作日早上 9 点
0 9 * * 1-5 - 每周一
0 0 * * 1 - 每月 1 号
0 0 1 * * - 每 6 小时
0 */6 * * * - 每周日零点
0 0 * * 0 - 每 2 小时
0 */2 * * * - 每 12 小时
0 */12 * * * - 每天中午 12 点
0 12 * * * - 每周五
0 0 * * 5 - 每月最后一天无法转换
- 每季度
0 0 1 */3 * - 每 2 分钟无法转换
- 每 3 小时
0 */3 * * * - 每天早上 6 点
0 6 * * * - 每天晚上 6 点
0 18 * * * - 每天两次
0 */12 * * * - 工作日晚上 6 点
0 18 * * 1-5 - 周末上午 10 点
0 10 * * 0,6 - 每月 15 号
0 0 15 * * - 每月第一个周一无法转换
- 每月最后一个周五无法转换
- 每年一次
0 0 1 1 * - 每 30 秒无法转换
- 每 3 分钟无法转换
- 每 20 分钟
*/20 * * * * - 每 4 小时
0 */4 * * * - 每 8 小时
0 */8 * * * - 每天凌晨 1 点
0 1 * * * - 每天凌晨 2 点
0 2 * * * - 每天凌晨 3 点
0 3 * * * - 每天早上 8 点
0 8 * * * - 每个工作日
0 0 * * 1-5 - 每个周末
0 0 * * 0,6 - 每周二
0 0 * * 2 - 每周三
0 0 * * 3 - 每周四
0 0 * * 4 - 每周六
0 0 * * 6 - 每 6 个月
0 0 1 */6 * - 每 10 秒无法转换
- 每 5 秒无法转换
GitHub Actions 的坑
最短 5 分钟GitHub 文档写明定时工作流最短每 5 分钟一次,不要指望更短。
可能延迟或被丢弃高峰期定时运行会延迟,尤其是整点,部分运行可能被丢弃。时间敏感就别用第 0 分钟。
不设 timezone 就是 UTC在 schedule 条目里加 timezone: 和 IANA 时区名。不加就按 UTC。
只在默认分支运行,还会被停用定时任务用默认分支的最新提交运行。公开仓库 60 天没有活动会被自动停用。
与 GitHub Actions 互转
GitHub Actions cron 转 crontabcrontab cron 转 GitHub ActionsGitHub Actions cron 转 KubernetesKubernetes cron 转 GitHub ActionsGitHub Actions cron 转 AWS EventBridgeAWS EventBridge cron 转 GitHub ActionsGitHub Actions cron 转 SpringSpring cron 转 GitHub ActionsGitHub Actions cron 转 QuartzQuartz cron 转 GitHub Actions
常见问题
GitHub Actions 的定时任务按哪个时区运行?
默认 UTC。在 schedule 条目里 cron: 旁边加 timezone:,写 IANA 时区名,比如 Asia/Shanghai。
GitHub Actions 能每分钟运行吗?
不能。文档写的最短间隔是 5 分钟。更短的间隔请从外部用 workflow_dispatch 或 repository_dispatch 触发。
GitHub Actions 支持 @hourly、@daily 吗?
不支持。GitHub 不支持 @yearly、@monthly、@weekly、@daily、@hourly 和 @reboot,请写五个字段。