Callog GitHub

Blog

GitHub Actions to Slack on failure: how to @mention the person who broke the build

It's Friday afternoon, and main has been red since lunch. The alert went out right on time, into a channel with forty people in it... and every one of them figured somebody else had it. Nobody was being lazy. The alert just didn't belong to anyone.

You know the channel. #ci-alerts, or #builds, or whatever yours is called. Somebody set it up with good intentions, and now it's a wall of red that everyone has muted.

A message that's addressed to everybody is addressed to nobody. It's like shouting "someone left the stove on!" into a busy office. Everyone assumes someone else will check.

What actually works is boring: tag the one person who can fix it. Usually that's whoever wrote the commit that broke the build. They've still got the change in their head, they know what they touched, and a direct ping gets read.

Most "GitHub Actions Slack notification" guides stop right before this part. Let's not.

Why @username in the message doesn't work

First trap, and almost everyone hits it. You write @octocat in the payload, the message shows up, and... nothing. No ping. Slack just prints the text.

That's because Slack doesn't match names in text. A real mention is the person's Slack user ID wrapped in angle brackets:

{ "text": "Build failed on main. <@U024BE7LH>, can you take a look?" }

So the job is really two jobs. Figure out who broke the build — you'll get a GitHub login, maybe an email. Then turn that into a Slack ID like U024BE7LH.

The first half is easy. The second half is where it gets messy, and where most setups quietly rot.

Step 1: send a Slack message when a job fails

Let's get a plain alert working before we get clever. Put it in its own job that waits for the jobs you care about. With needs, if: failure() runs it when any of them failed.

.github/workflows/ci.yml
name: CI
on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: npm ci && npm test

  notify-failure:
    needs: [test]
    if: failure()
    runs-on: ubuntu-latest
    steps:
      - name: Post to Slack
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
          REF: ${{ github.ref_name }}
        run: |
          jq -n --arg text "CI failed on \`$REF\`: $RUN_URL" '{text: $text}' \
            | curl -sf -X POST -H 'Content-Type: application/json' --data @- "$SLACK_WEBHOOK_URL"

Two small habits in there that'll save you a bad afternoon later.

The JSON is built with jq, not glued together in a string. Branch names and commit messages have quotes in them more often than you'd think, and one stray " breaks the whole payload.

And the GitHub context goes through env instead of straight into the script. A branch name or a PR title is user input. Pasting it into a shell command is a script injection hole — a weird one to get bitten by, but people do.

Step 2: find who broke the build

Your first instinct will be github.actor. Mine was too. It's right there, it's a login, done.

Except it's the account that started the run. That's the person who clicked merge, or re-ran a flaky job, or Dependabot. So you end up pinging the reviewer who merged a teammate's PR, which is a great way to make a reviewer stop merging things.

The better source depends on the event:

Event GitHub login Email
pull_request github.event.pull_request.user.login not in the payload
push github.event.head_commit.author.username (empty if the email isn't linked to an account) github.event.head_commit.author.email
anything else github.actor as a fallback —

In the workflow, just take the first one that's set:

env:
  AUTHOR_LOGIN: ${{ github.event.pull_request.user.login || github.event.head_commit.author.username || github.actor }}
  AUTHOR_EMAIL: ${{ github.event.head_commit.author.email }}

One wrinkle. A push to main can carry several commits, and head_commit is only the last one. In practice that's fine — if you squash-merge, it's the PR author, which is who you want anyway.

Step 3: turn a GitHub user into a Slack user ID

Here's the part nobody likes. There are two common ways to do it. Both work great with three people and one repo. Both get worse every month after that.

Option A: a mapping file in the repo

The low-tech route. A JSON file that says "this GitHub login is this Slack ID". To get someone's ID, open their Slack profile, hit the ⋮ menu, and choose "Copy member ID".

.github/slack-users.json
{
  "octocat": "U024BE7LH",
  "hubot": "U0G9QF9C6"
}

Then look the author up, and fall back to their plain login if they're not in the file:

      - uses: actions/checkout@v7
        with:
          sparse-checkout: .github/slack-users.json
          sparse-checkout-cone-mode: false
      - name: Post to Slack
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          AUTHOR_LOGIN: ${{ github.event.pull_request.user.login || github.event.head_commit.author.username || github.actor }}
          RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
          REF: ${{ github.ref_name }}
        run: |
          SLACK_ID=$(jq -r --arg login "$AUTHOR_LOGIN" '.[$login] // empty' .github/slack-users.json)
          if [ -n "$SLACK_ID" ]; then WHO="<@$SLACK_ID>"; else WHO="$AUTHOR_LOGIN (no Slack mapping)"; fi
          jq -n --arg text "CI failed on \`$REF\`. $WHO, can you take a look? $RUN_URL" '{text: $text}' \
            | curl -sf -X POST -H 'Content-Type: application/json' --data @- "$SLACK_WEBHOOK_URL"

Honestly, for a small team this is fine. No Slack app, nothing to approve, you can read the whole thing in ten seconds.

The catch shows up slowly. Every new hire is a PR. Every person who leaves is a PR nobody remembers to open. And the moment someone copies the file into a second repo, you've got two lists that'll disagree by spring.

Option B: look people up by email

The "smarter" route. Slack can find a user by email with users.lookupByEmail. The catch is you need a real Slack app with a bot token and the users:read and users:read.email scopes. A plain incoming webhook can't do this.

      - name: Find the Slack user
        id: slack-user
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
          AUTHOR_EMAIL: ${{ github.event.head_commit.author.email }}
        run: |
          ID=$(curl -sf -G https://slack.com/api/users.lookupByEmail \
                 -H "Authorization: Bearer $SLACK_BOT_TOKEN" \
                 --data-urlencode "email=$AUTHOR_EMAIL" | jq -r '.user.id // empty')
          echo "id=$ID" >> "$GITHUB_OUTPUT"

Then pass ${{ steps.slack-user.outputs.id }} to the next step through env — same habit as before — and put <@$SLACK_ID> into the message whenever it isn't empty.

It feels like magic the first time. You test it on yourself, it works, you ship it. Then, a few weeks later, you notice some people just never get tagged. The email in their commits isn't the email Slack knows:

  • Noreply addresses. Lots of developers commit as [email protected] to keep their real address private. Slack has never heard of it.
  • Personal email. That laptop set up years ago still commits as [email protected]. Slack knows [email protected].
  • PR events have no email at all. The pull_request payload gives you a login, not an address, so you need another GitHub API call — which can come back empty too.
  • Contractors and guests on a different domain don't match either.

And here's the sneaky bit. When the lookup fails, the alert still goes out. It just doesn't tag anyone. Nothing errors, nothing looks broken... until the one failure that mattered sits there untouched for a day.

When one repo turns into twenty

With one repo, either option is fine, really. Pick whichever annoys you less.

It's at twenty repos that things get strange — and the YAML isn't the problem. The mapping now lives in twenty places and slowly drifts apart, and you only find out whose mention is broken when someone asks why they never get pinged. Meanwhile every one of those repos is holding a Slack webhook or a bot token, which is twenty secrets to rotate. (Webhook URLs leak more than people expect. That's a whole other post.)

The fix is less about code and more about where things live. Pull the mapping and the Slack credentials out of the repos and into one place. Workflows then just say "this job failed, here's the commit", and that one place decides who to tag and where to post. Kind of like having one front desk instead of every room in the building running its own phone line.

How Callog does it

This is the exact itch Callog was built to scratch. It's open source and you host it yourself. The workflow sends an event through a GitHub Action and signs in with the job's own GitHub OIDC token. So the repo doesn't hold a Slack secret at all:

.github/workflows/ci.yml
permissions:
  id-token: write
  contents: read

jobs:
  notify-failure:
    needs: [test]
    if: failure()
    runs-on: ubuntu-latest
    steps:
      - uses: callog-dev/callog/packages/[email protected]
        with:
          url: https://notify.example.com
          route: ci-failed
          mention-author: true
          data: |
            { "branch": "${{ github.ref_name }}",
              "run_url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" }

With mention-author: true, Callog finds the commit or PR author and tags the person linked to that GitHub account. People come in from Slack and get matched by a verified company email or by repeated commits from one address. Anyone whose match is off can fix it themselves with /notify link github. And if someone can't be matched, they show up on a list instead of silently not getting tagged — which, after the email section above, is kind of the whole point. Where alerts go and what they look like lives in one config repo and changes through pull requests.

That said: if you've got a handful of repos, the mapping file from Option A is a perfectly good place to stop. No shame in it. It's when you hit dozens that one shared place starts paying for itself, whether that's Callog or something you build in-house.

The short version

Slack only tags people by user ID, <@U…>, so a name in the text is just text. And github.actor is whoever started the run, not whoever wrote the code — reach for the PR author or head_commit instead.

After that it's a trade-off. A mapping file is dead simple and drifts every time someone joins. Email lookup feels automatic and quietly misses everyone with a noreply or personal address. Once you're past a few repos, keep the mapping and the Slack credentials in one place, so the alert reaches the person who can actually fix the build.

Try Callog

Callog is open source. It routes CI alerts from one git repo to Slack and Telegram and mentions the person who broke the build, with no webhook secrets in your repositories.