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
scheduletrigger 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_dispatchtoo 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
How to Schedule a Nightly Build in GitHub ActionsRun a nightly GitHub Actions build with on.schedule and a cron expression, useful for regression suites, depe…
How to Post a Slack Message on Failure in GitHub ActionsSend a Slack notification when a GitHub Actions workflow fails by adding a step guarded with if failure() tha…