How to run a GitHub Action on a schedule
January 9, 2022
You can use GitHub Actions as a way to run a cron job script, which theyāll run for you for free (as long as you stay within the monthly limits).
Letās say Iām creating a GitHub Action that runs a Node.js script on a schedule. Iāll call the Action āScheduled Jobā, but you can call it whatever youād like.
Iāll first create a file at .github/workflows/scheduled-job.yaml
(you can replace scheduled-job
with whatever name youād like).
Then, hereās the contents of that file:
name: Scheduled Job
on:
schedule:
- cron: "0 * * * *"
- cron: "*/15 8-10 * * *"
jobs:
scheduled-job:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- run: npm install
- run: node path-to-your-script.js
The on: schedule
part tells GitHub Actions when to run this job, using cron syntax.
You can define multiple schedules! In this example, Iāve instructed the job to run every hour, but also every 15 minutes for 8-10am. These times are all in UTC, and at the moment thereās no way to change the timezone for the scheduler.
Also, be aware that GitHub Actions only runs scheduled jobs on the default branch of your repository (usually master
or main
).
Subscribe to my newsletter!
A weekly round-up of new blog posts and updates to projects Iām working on.