Back to Notes
How To
How to Run Scheduled Cron Jobs in GitHub Workflows for Free
March 28, 2024
Learn how to run cron jobs and schedule recurring tasks in GitHub Actions workflows for free.
As a programmer, there are often tasks that need to be run on a regular schedule, such as syncing data from an API, sending notifications, or generating reports. In a traditional server environment, you might use a cron job to automate these recurring tasks. But what if you're running a serverless application or want to avoid the overhead of managing a separate server just for cron jobs?
That's where GitHub Actions comes in! With GitHub Actions, you can define workflows that run on a schedule, allowing you to execute scripts or commands at specified intervals, all for free!
In this post, I'll walk you through how to set up a scheduled workflow in GitHub Actions to run a cron job. We'll use a simple example of syncing data from an API and committing the updated data to our repository.
Here's the workflow file (.github/workflows/sync-db.yml
) that defines our monthly cron job:
name: Monthly Sync DB
on:
schedule:
- cron: '0 0 1 * *' # Runs at 00:00 UTC on the 1st day of every month
workflow_dispatch:
jobs:
syncDB:
runs-on: ubuntu-latest
permissions:
# Give the GITHUB_TOKEN write permission to commit changes
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run sync-db.js
run: node src/utils/sync-db.js
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update shows.json
file_pattern: src/data/shows.json
commit_options: '--no-verify --signoff'
Let me break this down step-by-step:
Define the schedule: The
on
section specifies when the workflow should run. In our case, we use theschedule
event with a cron syntax of0 0 1 * *
. This means the job will run at 00:00 UTC (midnight) on the 1st day of every month.Checkout code: The first step checks out a copy of our repository's code to the GitHub Actions runner.
Set up Node.js: Since our example uses a Node.js script, we set up the Node.js environment using the
setup-node
action.Install dependencies: We run
npm ci
to install our project's dependencies.Run sync script: Next, we execute our
sync-db.js
script, which fetches data from an API and updates our localshows.json
file.Commit changes: Finally, we use the
git-auto-commit-action
to automatically commit any changes made to theshows.json
file and push them back to our repository.
And that's it! With this workflow, our data will stay up-to-date by automatically syncing on the 1st of every month, all without needing to manage a separate server.
The best part? GitHub Actions includes a generous free tier, so you can run these scheduled workflows at no cost for most use cases.
Of course, this is just a simple example. You can easily modify the workflow to run scripts in other languages, sync data from different sources, or execute any other recurring tasks your applications might need.
So go ahead and give it a try! Automating cron jobs with GitHub Actions can streamline your development workflow and simplify your app's architecture, all while leveraging the power of GitHub's robust CI/CD platform.
As a programmer, there are often tasks that need to be run on a regular schedule, such as syncing data from an API, sending notifications, or generating reports. In a traditional server environment, you might use a cron job to automate these recurring tasks. But what if you're running a serverless application or want to avoid the overhead of managing a separate server just for cron jobs?
That's where GitHub Actions comes in! With GitHub Actions, you can define workflows that run on a schedule, allowing you to execute scripts or commands at specified intervals, all for free!
In this post, I'll walk you through how to set up a scheduled workflow in GitHub Actions to run a cron job. We'll use a simple example of syncing data from an API and committing the updated data to our repository.
Here's the workflow file (.github/workflows/sync-db.yml
) that defines our monthly cron job:
name: Monthly Sync DB
on:
schedule:
- cron: '0 0 1 * *' # Runs at 00:00 UTC on the 1st day of every month
workflow_dispatch:
jobs:
syncDB:
runs-on: ubuntu-latest
permissions:
# Give the GITHUB_TOKEN write permission to commit changes
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run sync-db.js
run: node src/utils/sync-db.js
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update shows.json
file_pattern: src/data/shows.json
commit_options: '--no-verify --signoff'
Let me break this down step-by-step:
Define the schedule: The
on
section specifies when the workflow should run. In our case, we use theschedule
event with a cron syntax of0 0 1 * *
. This means the job will run at 00:00 UTC (midnight) on the 1st day of every month.Checkout code: The first step checks out a copy of our repository's code to the GitHub Actions runner.
Set up Node.js: Since our example uses a Node.js script, we set up the Node.js environment using the
setup-node
action.Install dependencies: We run
npm ci
to install our project's dependencies.Run sync script: Next, we execute our
sync-db.js
script, which fetches data from an API and updates our localshows.json
file.Commit changes: Finally, we use the
git-auto-commit-action
to automatically commit any changes made to theshows.json
file and push them back to our repository.
And that's it! With this workflow, our data will stay up-to-date by automatically syncing on the 1st of every month, all without needing to manage a separate server.
The best part? GitHub Actions includes a generous free tier, so you can run these scheduled workflows at no cost for most use cases.
Of course, this is just a simple example. You can easily modify the workflow to run scripts in other languages, sync data from different sources, or execute any other recurring tasks your applications might need.
So go ahead and give it a try! Automating cron jobs with GitHub Actions can streamline your development workflow and simplify your app's architecture, all while leveraging the power of GitHub's robust CI/CD platform.
As a programmer, there are often tasks that need to be run on a regular schedule, such as syncing data from an API, sending notifications, or generating reports. In a traditional server environment, you might use a cron job to automate these recurring tasks. But what if you're running a serverless application or want to avoid the overhead of managing a separate server just for cron jobs?
That's where GitHub Actions comes in! With GitHub Actions, you can define workflows that run on a schedule, allowing you to execute scripts or commands at specified intervals, all for free!
In this post, I'll walk you through how to set up a scheduled workflow in GitHub Actions to run a cron job. We'll use a simple example of syncing data from an API and committing the updated data to our repository.
Here's the workflow file (.github/workflows/sync-db.yml
) that defines our monthly cron job:
name: Monthly Sync DB
on:
schedule:
- cron: '0 0 1 * *' # Runs at 00:00 UTC on the 1st day of every month
workflow_dispatch:
jobs:
syncDB:
runs-on: ubuntu-latest
permissions:
# Give the GITHUB_TOKEN write permission to commit changes
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run sync-db.js
run: node src/utils/sync-db.js
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update shows.json
file_pattern: src/data/shows.json
commit_options: '--no-verify --signoff'
Let me break this down step-by-step:
Define the schedule: The
on
section specifies when the workflow should run. In our case, we use theschedule
event with a cron syntax of0 0 1 * *
. This means the job will run at 00:00 UTC (midnight) on the 1st day of every month.Checkout code: The first step checks out a copy of our repository's code to the GitHub Actions runner.
Set up Node.js: Since our example uses a Node.js script, we set up the Node.js environment using the
setup-node
action.Install dependencies: We run
npm ci
to install our project's dependencies.Run sync script: Next, we execute our
sync-db.js
script, which fetches data from an API and updates our localshows.json
file.Commit changes: Finally, we use the
git-auto-commit-action
to automatically commit any changes made to theshows.json
file and push them back to our repository.
And that's it! With this workflow, our data will stay up-to-date by automatically syncing on the 1st of every month, all without needing to manage a separate server.
The best part? GitHub Actions includes a generous free tier, so you can run these scheduled workflows at no cost for most use cases.
Of course, this is just a simple example. You can easily modify the workflow to run scripts in other languages, sync data from different sources, or execute any other recurring tasks your applications might need.
So go ahead and give it a try! Automating cron jobs with GitHub Actions can streamline your development workflow and simplify your app's architecture, all while leveraging the power of GitHub's robust CI/CD platform.
As a programmer, there are often tasks that need to be run on a regular schedule, such as syncing data from an API, sending notifications, or generating reports. In a traditional server environment, you might use a cron job to automate these recurring tasks. But what if you're running a serverless application or want to avoid the overhead of managing a separate server just for cron jobs?
That's where GitHub Actions comes in! With GitHub Actions, you can define workflows that run on a schedule, allowing you to execute scripts or commands at specified intervals, all for free!
In this post, I'll walk you through how to set up a scheduled workflow in GitHub Actions to run a cron job. We'll use a simple example of syncing data from an API and committing the updated data to our repository.
Here's the workflow file (.github/workflows/sync-db.yml
) that defines our monthly cron job:
name: Monthly Sync DB
on:
schedule:
- cron: '0 0 1 * *' # Runs at 00:00 UTC on the 1st day of every month
workflow_dispatch:
jobs:
syncDB:
runs-on: ubuntu-latest
permissions:
# Give the GITHUB_TOKEN write permission to commit changes
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run sync-db.js
run: node src/utils/sync-db.js
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update shows.json
file_pattern: src/data/shows.json
commit_options: '--no-verify --signoff'
Let me break this down step-by-step:
Define the schedule: The
on
section specifies when the workflow should run. In our case, we use theschedule
event with a cron syntax of0 0 1 * *
. This means the job will run at 00:00 UTC (midnight) on the 1st day of every month.Checkout code: The first step checks out a copy of our repository's code to the GitHub Actions runner.
Set up Node.js: Since our example uses a Node.js script, we set up the Node.js environment using the
setup-node
action.Install dependencies: We run
npm ci
to install our project's dependencies.Run sync script: Next, we execute our
sync-db.js
script, which fetches data from an API and updates our localshows.json
file.Commit changes: Finally, we use the
git-auto-commit-action
to automatically commit any changes made to theshows.json
file and push them back to our repository.
And that's it! With this workflow, our data will stay up-to-date by automatically syncing on the 1st of every month, all without needing to manage a separate server.
The best part? GitHub Actions includes a generous free tier, so you can run these scheduled workflows at no cost for most use cases.
Of course, this is just a simple example. You can easily modify the workflow to run scripts in other languages, sync data from different sources, or execute any other recurring tasks your applications might need.
So go ahead and give it a try! Automating cron jobs with GitHub Actions can streamline your development workflow and simplify your app's architecture, all while leveraging the power of GitHub's robust CI/CD platform.