Cron scheduling is a useful feature in GitHub Actions that allows you to run a workflow on a schedule. This can be useful for tasks such as running tests or deploying code at regular intervals.
To use cron scheduling in GitHub Actions, you will need to add a schedule key to your workflow file. The schedule key should contain a cron expression that specifies when the workflow should run.
Here is an example of a workflow that runs every day at noon:
name: Daily Build
on:
schedule:
- cron: '0 12 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: npm test
In this example, the cron expression 0 12 * * * means that the workflow will run at noon (12:00) every day. The expression consists of five fields separated by spaces:
0: The minute field. This specifies that the workflow should run at the 0th minute of the hour (i.e., on the hour). 12: The hour field. This specifies that the workflow should run at 12:00 (noon). *: The day of the month field. The asterisk means that the workflow will run every day of the month. *: The month field. The asterisk means that the workflow will run every month. *: The day of the week field. The asterisk means that the workflow will run every day of the week. You can use a variety of different values in the fields of a cron expression to specify when the workflow should run. For example, you could use 0 0 * * 1 to run the workflow at midnight every Monday, or 0 0 1 * * to run the workflow at midnight on the first of every month.
It’s also possible to use more advanced cron expressions with multiple schedules. For example:
on:
schedule:
- cron: '0 12 * * 1,2,3,4,5'
- cron: '0 12 * * 6'
- cron: '0 0 1 * *'
In this example, the workflow will run at noon on weekdays (Monday through Friday) and at midnight on the first of every month.
GitHub Actions provides a helpful cron expression builder tool that you can use to create custom cron expressions. You can access the tool by clicking the “Add a new schedule” button on the Actions tab of your repository.
I hope this helps you get started with cron scheduling in GitHub Actions! Let me know if you have any questions.