novirael's logo
Published on

Github Actions Failure Notification Hacks

Github Actions Failure Notification Hacks

Abstract

A growing codebase can unexpectedly turn simple tasks with simple solutions into simple tasks that need sophisticated solutions. This article describes how we can and cannot implement failure notifications with GitHub Actions.

Requirements

Our team is notified on the Slack channel when the main branch is unstable.

Failure Notification Requirements

Level 0: Very simple use case

Starting simple. We want to build an application after the merge to the main branch.

Simple Workflow
jobs:
 build:
  runs-on: ubuntu-latest
  steps:
   - uses: actions/checkout@v4
   - name: Build
    run: echo "Build code"
   - name: Slack Notification
    if: failure()
    uses: rtCamp/action-slack-notify@v2
    env:
      SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}

Note the condition used for our notification. Based on the documentation 'if: failure()' will be called if any previous step fails.

Level 2: More complex scenario

We still want to build the application after merging to the main branch, but before doing that we would like to run the tests. If tests are failing we would like to skip the build.

Upgrading Workflow

Analysis of cases:

  • tests pass, build pass: No notification triggered
  • tests pass, build fails: Notification triggered as expected
  • tests fails, build ???: Build job cancelled, no notification sent

As we can see this scenario cannot work. The only way to meet our requirement is to duplicate the code related to Slack notification.

Level 3: Well-decomposed workflow

What if your workflow is a bit more complex? Some of my workflows consist of four, five or even more jobs. Making sure that your well-decomposed workflow covers failure scenarios might be a challenge.

Decomposed Workflow
Decomposed Workflow Example

What we could do there is reduce duplication using the GitHub Actions reusable workflows feature. This solution is not ideal, but probably makes the most sense in given circumstances.