Kasrel
Kasrel

Reputation: 197

Trigger github action on schedule, not CI push

Hopefully an easy one. I need to trigger slacknotification for failed jobs triggered by a cron schedule in GitHub actions. The script forces a failure to test slacknotification functionality. No matter what I try, I can't seem to get the scheduled run to trigger. This script resides in the main branch so it should trigger scheduled jobs.

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

jobs:
  test-fail:
    runs-on: ubuntu-latest
    steps:
    - name: force fail
      run: exit 1

  test-skip:
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    steps:
    - name: force fail
      run: exit 1

  slackNotification:
    if: ${{ github.event_name == 'schedule' && (needs.test-fail.result == 'failure' || needs.test-skip.result == 'failure') }}
    needs: [test-fail, test-skip]...

on slackNotification: if: ... line, notification will work if I use always() instead of github.event_name == 'schedule but that will trigger with CI push as well. I only want to trigger the action if schedule event fails. There are a bunch of these scattered across stackoverflow like this one, but they don't quite cover this exact scenario.

Am I close?

Upvotes: 2

Views: 878

Answers (1)

Kasrel
Kasrel

Reputation: 197

I found a very helpful solution here which dealt with the same issue. Messing around with the script a little further, I managed to get the desired result. I needed to call always() as well. I also needed to be patient for actions to schedule and execute the script.

if: ${{ (always() && github.event_name == 'schedule') && ( needs... }}

Upvotes: 1

Related Questions