Distinct job for each schedule

I have a backup.yml workflow which I use for automated database backup. I’ve started with a workflow that schedules hourly, however, I want to include in a single workflow multiple backup jobs. Hourly, daily, weekly, etc.

Is there a way for me to conditionally skip a job in the workflow if the workflow was not triggered by the desired schedule? For example:

jobs:
  hourly:
    if: invoked by an hourly trigger
  daily:
    if: invoked by a daily trigger

  

Hi @mhitza ,

Thank you for being here! If you’d like a single workflow to include multiple schedule jobs, you can add multiple ‘corn’ expression for schedule event, sample as below, you can use ‘cron: ‘*/60 * * * *’’ for hourly…etc.(doc here)

on:
  schedule:
    - cron: '*/5 * * * *' # every 5 mins
    - cron: '*/7 * * * *' # every 7 mins

For different schedule, the github has different context, for intance, 7mins schedule:

schedulecheck.png

You can use if expression to conditionally skip the other jobs then.

on:
  schedule:
    - cron: '*/5 * * * *'
    - cron: '*/7 * * * *'
jobs:
  job1:
    if: contains(github.event.schedule, '*/7 * * * *')
    runs-on: [ubuntu-latest]
    steps:
      - name: response when schedule 7 mins
        run: |
          echo this is 7 minutes schedule
  job2:
    if: contains(github.event.schedule, '*/5 * * * *')
    runs-on: [ubuntu-latest]
    steps:
      - name: response when schedule 5 mins
        run: |
          echo this is 5 minutes schedule

Hope it helps.

3 Likes

Adding this to the official docs would be mighty helpful!