Ben Borgers

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).

More blog posts

Subscribe to my newsletter for a monthly round-up of new blog posts and projects Iā€™m working on!

Twitter ↗ RSS feed ↗