logo

Creating a Cron Job with GitHub Actions

O

Ohidur Rahman Bappy

MAR 22, 2025

Creating a Cron Job with GitHub Actions

In this guide, you'll learn how to set up a cron job using GitHub Actions to automate tasks and schedule regular operations.

How to Set Up a Cron Job

GitHub Actions allows you to schedule jobs at regular intervals using cron syntax. Below is an example configuration for a cron job that runs every 15 minutes.

name: 15-minute-cron
on:
  schedule:
    - cron: '*/15 * * * *'
jobs:
  cron:
    runs-on: ubuntu-latest
    steps:
      - name: Call our API route
        run: |
          curl --request POST \
          --url 'https://yoursite.com/api/cron' \
          --header 'Authorization: Bearer ${{ secrets.YOUR_API_KEY }}'

Explanation of the Workflow

  • name: This defines the name of your workflow. In this example, it's 15-minute-cron.
  • on.schedule: This uses cron syntax to set the schedule. */15 * * * * means the job will run every 15 minutes.
  • jobs.cron.runs-on: Specifies the environment for the job. Here, it's set to ubuntu-latest.
  • steps: This section defines what the job will do. In this case, it makes a POST request to an API endpoint.

Using Secrets

The Authorization: Bearer ${{ secrets.YOUR_API_KEY }} header uses a GitHub secret to keep your API key secure. Be sure to set this up in your GitHub repository under Settings > Secrets.

Conclusion

With this setup, you can leverage GitHub Actions to automate tasks and keep processes running smoothly with minimal manual intervention. Cron jobs are a powerful way to maintain and streamline your workflow.