Skip to content
Latchkey

How to Run Tests on a Schedule and Alert on Failure in GitHub Actions

Flaky tests and dependency drift surface overnight, but only if something runs the suite and tells you when it breaks.

Trigger the workflow with schedule and gate a Slack notification step on failure() so a passing run produces no noise.

Steps

  • Add a schedule trigger with a cron expression (UTC).
  • Run the test suite as usual.
  • Add a notification step guarded by if: failure() so it only fires on a red run.
  • Keep workflow_dispatch too so you can run it on demand.

Workflow

.github/workflows/nightly.yml
name: Nightly Tests
on:
  schedule:
    - cron: '0 6 * * *'
  workflow_dispatch:
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with: { node-version: '20' }
      - run: npm ci
      - run: npm test
      - if: failure()
        uses: slackapi/slack-github-action@v2
        with:
          webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
          webhook-type: incoming-webhook
          payload: |
            text: "Nightly tests failed on ${{ github.repository }} (${{ github.run_id }})"

Notes

  • Scheduled runs always use the default branch; cron does not respect branch context.
  • GitHub may delay scheduled runs under load, so avoid exact top-of-hour times for time-sensitive jobs.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →