DEV Community

Cover image for Schedule GitHub Actions Using CRON Expressions
Sarah Lean 🏴󠁧󠁒
Sarah Lean 🏴󠁧󠁒

Posted on β€’ Originally published at techielass.com

3 3

Schedule GitHub Actions Using CRON Expressions

You can trigger GitHub Actions to run on a number of different events. The most common would be running when you push changes to your repository. Also you may want to trigger an Action to run on pull requests.

When building Actions you may want to run them on a defined schedule. This tutorial shows you how to configure a GitHub Action on a given schedule using CRON expressions.

Can you schedule GitHub Actions?

Yes, yes you can. You have the option to trigger a GitHub Actions on a schedule an example could be:

name: Test Build

on:  
  push:
  pull_request:
  schedule:
    - cron: '00 1 * * 1'  # At 01:00 on Mondays.

Enter fullscreen mode Exit fullscreen mode

What is CRON?

CRON is a command line utility that is used to schedule jobs. You'll hear it referred to CRON jobs or CRON tasks.

The CRON syntax can sometimes be confusing when first encountered. There are five sections that can be configured within the CRON syntax. You can specify day of week, month, day of the month, hour and minute.

This isn't something you need to commit to memory but being able to read the syntax is useful. Below is a great diagram to show you how the syntax is broken down.

# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ minute (0 - 59)
# β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ hour (0 - 23)
# β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the month (1 - 31)
# β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ month (1 - 12)
# β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ day of the week (0 - 6)
# β”‚ β”‚ β”‚ β”‚ β”‚                       
# β”‚ β”‚ β”‚ β”‚ β”‚
# β”‚ β”‚ β”‚ β”‚ β”‚
# * * * * * <command to execute>
Enter fullscreen mode Exit fullscreen mode

I love to use the website https://crontab.guru/. You can put the syntax in to the website and it will decipher what that schedule would do, or equally you can use it to build the right syntax for the schedule you want to execute.

What is the GitHub Actions CRON syntax?

I want to show you an example of a GitHub Actions workflow including a CRON schedule.

name: Trigger Action on a CRON Schedule

on:
  schedule:
    # Runs "At 11:00 on every day-of-week from Monday through Friday"
    - cron: '0 11 * * 1-5'

jobs:
  build:
    name: Trigger Code Checkout
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
Enter fullscreen mode Exit fullscreen mode

The above GitHub Actions workflow will open trigger on a schedule. The CRON job I have set will run at 11am on every day-of-week from Monday through Friday.

The workflow then will run the Code Checkout step on a Ubuntu runner.

It's a straight forward workflow, but it triggers using a CRON schedule rather than on a push to GitHub or on a pull request.

Be sure to check out the other blogs I have written on GitHub Action uses and features.

AWS Security LIVE! Stream

Go beyond the firewall

There's more to security than code. Explore solutions, strategies, and the full story on AWS Security LIVE!

Learn More

Latest comments (0)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes senseβ€”CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

πŸ‘‹ Kindness is contagious

Explore this insightful post in the vibrant DEV Community. Developers from all walks of life are invited to contribute and elevate our shared know-how.

A simple "thank you" could lift spiritsβ€”leave your kudos in the comments!

On DEV, passing on wisdom paves our way and unites us. Enjoyed this piece? A brief note of thanks to the writer goes a long way.

Okay