Schedule Tasks Using GitHub Actions

Himanshu Garg - Oct 29 '20 - - Dev Community

GitHub Actions can do a lot of things and can be triggered by different methods. Typically, you want a workflow to be executed on a push or a pull-request or an issue.

So, Where does scheduling a task fit in?

  • For Tracking good health of your software, you can perform scheduled testing.
  • Publishing the Nightly Build of your library, framework or software.

Scheduling doesn't require a change to be made into code. It will keep running on the unchanged or changed code on scheduled time. Scheduling a GitHub Workflow is done using cron. For those who don't know what cron is,

The software utility cron also known as a cron job is a time-based job scheduler in Unix-like computer operating systems.

Here is an example of scheduling a task.

# Give the workflow a name. 
name: Nightly Build

# Trigger Workflow every midnight UTC

on:
  schedule:
    - cron: '0 0 * * *'  
  workflow_dispatch: 
#workflow_dispatch helps run workflow anytime with a single click. 

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    name: Scheduling
    # make sure to run this action in Linux env (say ubuntu)
    runs-on: ubuntu-latest
    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Write all the steps you want to schedule
Enter fullscreen mode Exit fullscreen mode

For creating nightly build use create-release action with the above workflow. Make sure to use a dynamic naming format while creating tags & to set prerelease: true. Use the YYYYMMDD date format in the tag

Defining a cron schedule is a tougher task than one might expect. crontab.guru is a website which converts cron schedule into human-readable format. eg. 0 0 * * * means midnight according to UTC.

. .