How do I get a notification when my Vercel deployment fails?
Tags: devops, githubactions, vercel
Published: 2021-09-06 21:35:01

---
title: How do I get a notification when my Vercel deployment fails?
published: true
date: 2021-09-05 13:49:31 UTC
tags: DevOps,GitHubActions,Vercel
canonical_url: https://jasonstcyr.com/2021/09/05/how-do-i-get-a-notification-when-my-vercel-deployment-fails/
cover_image: https://theagilecoder.files.wordpress.com/2021/09/deployment-failures-1.png
---

My team was working on a project together and getting lots of ‘bot’ emails from our GitHub integration to Vercel. At least, we were getting emails when the build and deploy succeeded to Vercel.

**But what about when the deployment fails?**

Everything looked fine locally, the site was working, but after a few days of not getting email notifications we realized that Vercel was no longer deploying. Lots of errors in the logs. We had no idea! How do we get notified when something goes wrong on the Vercel side?

## This is “normal”.

When you have Vercel hooked into GitHub, a successful deployment triggers a comment (by the Vercel bot) back to the commit that caused the deployment. This is where the ‘success’ message goes, and why we were getting notifications. It wasn’t Vercel sending us the notification, it was GitHub letting us know that there was a comment on the commit.

It was confirmed for me by the Vercel team that it is entirely expected that Vercel does not put a comment back onto the Commit when there is a failure. What can you do out of the box?

1. You can see that the GitHub status checks are “all failed” on GitHub itself.
2. You can go to /deployments on your GitHub repo and see all the Vercel deployment attempts.
3. You can click through a given deployment to go to the Vercel side and find out why it failed.

**But still… there is no notification of a failure. And I wanted one!**

This seems like such a basic continuous integration scenario I couldn’t believe this hadn’t been covered somewhere. Somebody must be triggering deployments and figuring they’ll get an email if the ‘build’ goes red? Netlify [has a whole slew of notifications](https://docs.netlify.com/site-deploys/notifications/) that happen around site deploys.

I reached out [on the Vercel GitHub discussions board](https://github.com/vercel/vercel/discussions/6642), but got no responses. However, thanks to some helpful folks at Vercel who responded to an email, I was able to get some guidance.

## GitHub Actions to your rescue
I was recommended to look into GitHub actions to run custom workflows when specific deployment states occur. You can specify which state to run a specific job on, and then invoke the GitHub API.

> SIDE NOTE: Due to restrictions that can occur with private repos and your organization account, you may not have access to GitHub Actions. I had to work in a public repo to get this to work for me.

So, I decided to invoke the API for adding comments on a commit to mirror what the Vercel bot was doing on a success. [(View on GitHub)](https://github.com/jst-cyr/Vercel-GitHub-Actions/blob/main/.github/workflows/deploy-failed.yml)

```yaml
#Triggered for deployment status changes
name: Deployment Failure notification
on: [deployment_status]
 
jobs:
  add-failure-comment:
    name: Add a comment to the commit that caused the failure
    if: github.event.deployment_status.state == 'failure'
    runs-on: ubuntu-latest
 
    steps:
      - name: Add commit comment - failure
        run: |
          curl -L -X POST \
          --url https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments \
          --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
          --header 'content-type: application/json' \
          --data '{
            "body": "Deployment has failed."
            }'
```

## The important bits
The **if** statement here ensures that we tap into the deployment state and only do this on a failure:
```yaml
   if: github.event.deployment_status.state == 'failure'
```

The **curl** block invokes the GitHub API, specifically the one for **commit comments**.
```yaml
   curl -L -X POST \
         --url https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}/comments \
```

The **body** part of the **data** parameter is the content that will be put into the commit:
```yaml
   --data '{
            "body": "Deployment has failed."
            }'
```

And when a deployment fails it adds a comment like this:

[![Screenshot of comment on a commit reading Deployment has failed.](https://theagilecoder.files.wordpress.com/2021/09/image.png "Click to see comment on GitHub")](https://github.com/jst-cyr/Vercel-GitHub-Actions/commit/f5bc3b869bb98db51588287ad70eba3dee861ae7#commitcomment-55960321)

## Getting a notification
Alrighty, at this point, I had a comment being added to a commit… but I said I wanted a notification and that usually means people getting an email when the deployment fails, right?

The reason using the GitHub commit comment is nice is that it ties right into [GitHub’s normal notifications configuration](https://github.com/notifications). You can [update your settings on how you get notifications](https://github.com/settings/notifications) for things that happen on GitHub. In the **Email notification preferences** section you can configure what you want to get emails about.

So with this comment being added to the commit, you’ll get a notification when you break the deployment!

##Downloading the code
I have made the code available in a repo. If I think of other helpful actions, I’ll add those there too:

* [Vercel Deployment Failure Notification](https://github.com/jst-cyr/Vercel-GitHub-Actions/blob/main/.github/workflows/deploy-failed.yml) (jst-cyr GitHub repo)

##Things I read when trying to figure this out:

- [“How to get notified when Vercel deployment gets failed?](https://www.reddit.com/r/nextjs/comments/nve6hp/how_to_get_notified_when_vercel_deployment_gets/) (from NextJS Reddit, 2021-06-08)
- [Disable build email?](https://github.com/vercel/vercel/discussions/4268) (from Vercel GitHub, 2020-05-05)
- [Vercel API docs](https://vercel.com/docs/api#endpoints/deployments/list-deployments) – List deployments
- [Vercel for GitHub documentation](https://vercel.com/docs/git/vercel-for-github)
- [Vercel ‘Git’ configuration](https://vercel.com/docs/configuration#git)
- [Netlify site notifications](https://docs.netlify.com/site-deploys/notifications/)
- [Commit Comment repo](https://github.com/peter-evans/commit-comment) (by Peter Evans)
- [Workflow commands for GitHub Actions](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions) (GitHub docs)
- [Getting started with the REST API](https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api) (GitHub docs)
- [Working with comments](https://docs.github.com/en/rest/guides/working-with-comments) (GitHub docs)
- [Create a commit comment](https://docs.github.com/en/rest/reference/repos#create-a-commit-comment) (GitHub API reference)