Set env variables for scheduled workflow based on cron expression

I want to reuse my workflow jobs scheduling them at various times of the day but with different input
Basically something like this

on:
  schedule:
    - cron:  '0 0 * * *'
      env:
        MY_VAR: midnight_run
    - cron:  '0 6 * * *'
      env:
        MY_VAR: morning_run

but I assume this is not supported?

I can obviously run some custom command/script that will check current date and (assuming that I give it some tolerance in terms of exact minute) set the correct value of MY_VAR but it doesn’t feel like a good solution.

I’ll appreciate any ideas :slight_smile:

Hi @andrzej-kodify,

Glad to see you in Github Community Forum!

You cannot define top level env in the event, it’s not supported. As an alternative, based on the different schedule cron settting, you can use ‘set-env’ to set/update env value for the job.

Code sample as below:

on:
  schedule:
    - cron:  '*/6 * * * *'   # define cron setting
    - cron:  '*/7 * * * *'
jobs:
  jobenv:
    runs-on: ubuntu-latest
    steps:
      - name: setenv
        if: github.event.schedule=='*/6 * * * *'         # set-env value
        run: |
          echo "::set-env name=cronenv::midnight_run"
      - name: setenv2
        if: github.event.schedule=='*/7 * * * *'   # set env value
        run: |
          echo "::set-env name=cronenv::morning_run"
      - name: check env value  # check env value
        run: |
          echo $cronenv               

the var ‘cronenv’ has different value based on the different cron setting.
Confirmed it works on my side: https://github.com/weide-zhou/schedulewf1/runs/921576254?check_suite_focus=true#step:6:6

Thanks

2 Likes

Great, that’s the workaround I’ve been after. Thanks a lot :slight_smile: