Send notification when build status changes

I am attempting to migrate from Travis to Github Actions. One piece of functionality I have been unable to figure out how to migrate is:

email:
recipients:
- my-address@email.com
on\_failure: change
on\_success: change

Meaning, I want to send an email notification only when the build status changes from success to failure or vice-versa. Github Actions doesn’t seem to have a way to look up the previous build status. Any ideas?

3 Likes

Hi @cowwoc , 

I am sorry to tell you that there is not a similar function in GitHub Actions to send notification when build status change. 

In GitHub Actions, there is an option “Send notifications for failed workflows only” . The notification settings for GitHub Actions is in “https://github.com/settings/notifications” page.  There is the document for “Notifications for workflow runs” : https://help.github.com/en/actions/getting-started-with-github-actions/about-github-actions#notifications-for-workflow-runs 

I am also currently migrating to GitHub Actions.

Is there a way to change the notification address for only one repository? 
We have some tests, that are being executed periodically via GitHub Actions.

These Tests are hosted in their own repository and have their own CI pipeline. 

If they fail, our whole team should get notified about the failure, not only the person who last changed the test repository.
Is there a way to archieve this behaviour?

Ideally, the team should also get notified, once tests start succeeding again, but as you have mentioned, this is currently not supported. 

Hey @saltcreek-alex and @cowwoc 

What we do in our company is just send notifications in our slack channel. We have our custom solution by you can use this freely available slack notifier https://github.com/marketplace/actions/action-slack

You can also search for slack notifiers under the marketplace to check out other options.

1 Like

Hey Chris,

I think you misunderstood the question. We only want to receive notification when the build status changes states (from success to failure or vice-versa). We don’t want to receive consecutive success, failure notifications.

I’d like to point out that this is a feature that not only Travis, but also Jenkins offers.
Our Jenkins builds use post { fixed { ... } } to handle build recovery. This is especially useful for ensuring that our team is notified both when shared branches are broken and when they are fixed. I’m sure other CI services offer this too.

I’m currently looking into doing this by looking up the status history for the ref being built. This might be the API I need:

It’s going to require a custom action, but if I can get something working I’ll share it here.

1 Like

At work, we’re going to start using a snippet like this one to determine the last build for a branch and notify if the build status changes. We haven’t tested it incredibly thoroughly, but we’re going to start using it and see how it behaves on our builds. (Provided example is python, but it should adapt easily to other languages.)

def get_branchname():
    # split on slashes, strip 'refs/heads/*' , and rejoin
    # this is also the tag name if a tag is used
    return "/".join(os.environ["GITHUB_REF"].split("/")[2:])

def get_last_build_status():
    branch = get_branchname()
    repo = os.environ["GITHUB_REPOSITORY"]
    token = os.environ["GITHUB_TOKEN"]

    r = requests.get(
        f"https://api.github.com/repos/{repo}/actions/runs",
        params={"branch": branch, "status": "completed", "per_page": 1},
        headers={
            "Authorization": f"Bearer {token}",
            "Accept": "application/vnd.github.v3+json",
        },
    )
    print(f">> get past workflow runs params: branch={branch},repo={repo}")
    print(f">> get past workflow runs result: status={r.status_code}")
    runs_docs = r.json().get("workflow_runs", [])
    # no suitable status was found for a previous build, so the status is "None"
    if not runs_docs:
        print(">>> no previous run found for workflow")
        return None
    conclusion = runs_docs[0]["conclusion"]
    print(f">>> previous run found with conclusion={conclusion}")
    return conclusion

def check_status_changed(status):
    # NOTE: last_status==None is always considered a change. This is intentional
    last_status = get_last_build_status()
    res = last_status != status
    if res:
        print(f"status change detected (old={last_status}, new={status})")
    else:
        print(f"no status change detected (old={last_status}, new={status})")
    return res

I decided that writing the notification step as a script, rather than bothering about a custom action, was simpler.

I’d like for GitHub to provide this out of the box, maybe as one of their pre-built actions with an output, but I think this is going to work for now.