GitHub: Automate Periodic Tasks with GitHub Actions

Overview

This blog post introduces a simple GitHub Actions workflow designed to periodically perform tasks using a scheduled trigger. The workflow is triggered every 5 minutes to demonstrate how to automatically check out code and list the contents of a repository. This example can serve as a foundation for more complex automation tasks like periodic updates, automated testing, and monitoring across your projects.

Explanation with Inline Comments

name: Experiment  # Name of the workflow

on:
  schedule:
    # This schedule triggers the workflow every 5 minutes. The cron syntax is used here.
    - cron:  '*/5 * * * *'

jobs:
  # Define a job named 'demo'
  demo:
    runs-on: ubuntu-latest  # Specifies that the job should run on the latest Ubuntu runner provided by GitHub

    steps:
      - name: Checkout code  # Step for checking out the repository code
        uses: actions/checkout@v2  # Uses the checkout action at version 2

      - name: list files  # Step to list all files in the directory
        run: |  # The run command executes shell commands
          ls -lrth  # 'ls -lrth' lists files in human-readable format, sorted by modification time

Creating a schedule

Let’s start by defining a simple workflow named “Experiment” that runs every 5 minutes. We’ll use the cron syntax to specify the schedule.

name: Experiment

on:
  schedule:
    - cron: '*/5 * * * *'

Defining a Job

Within the workflow, we’ll define a job named “demo” that runs on the latest Ubuntu runner provided by GitHub.

jobs:
  demo:
    runs-on: ubuntu-latest

Adding Steps to the Job

Now, let’s add steps to the job. The first step checks out the repository code using the actions/checkout action.

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

The next step lists all files in the directory using a shell command.

  - name: list files
    run: |
      ls -lrth