GitHub Actions, a powerful feature of the GitHub platform, provides a seamless way to automate your workflows. One of the standout features within GitHub Actions is the ability to create cron jobs, enabling you to schedule recurring tasks for your pipelines. In this post, we’ll explore how GitHub Actions cron jobs can enhance your development process.

Why cron jobs?

Cron jobs are essential for scenarios where tasks need to be executed on a predefined schedule. Here are some use cases where GitHub Actions cron jobs shine:

  1. 1

    Running recurring reports

    Automate report generation and delivery without manual intervention.

  2. 2

    Exporting period-based activities

    Export data for analysis or create backups at regular intervals.

  3. 3

    Applying policies based on a schedule

    Enforce security scans, code quality checks, or other policy enforcement.

  4. 4

    Resource cleanup

    Delete obsolete branches, clean up temporary files, or manage cloud resources.

Crafting your cron job

Let’s dive into the practical side of things. Creating a cron job in GitHub Actions involves specifying a cron expression in your workflow file. Here’s a simple example:

name: Cron Job Example

# +-------------------------------------------+
# |          GitHub Actions Cron Job          |
# |                                           |
# |    Schedule: 0 0 * * * (Every midnight)   |
# |                                           |
# |       +-- minute (0 - 59)
# |       |  +-- hour (0 - 23)
# |       |  |  +-- day of month (1 - 31)
# |       |  |  |  +-- month (1 - 12)
# |       |  |  |  |  +-- day of week (0 - 6)
# |       |  |  |  |  |
# |       0  0  *  *  *
# +-------------------------------------------+

on:
  schedule:
    # Example cron expressions:
    # - '0 0 * * *'   # Run every day at midnight UTC
    # - '15 3 * * *'  # Run every day at 3:15 AM UTC
    # - '0 */6 * * *' # Run every 6 hours
    - cron: '0 0 * * *'  # Run every day at midnight UTC

jobs:
  cron_job:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      # Add your job steps here
      - name: Run Your Cron Job
        run: |
          echo "Running cron job"
          # Add your actual cron job commands here

Putting it into action

Now that you have your cron job set up, GitHub Actions will automatically execute your workflow based on the defined schedule. Whether it’s daily, weekly, or any other interval, you can rely on GitHub Actions to keep your tasks running smoothly.