# Buildkite build finished notifications on your phone

> Get a High-priority push notification when a Buildkite build fails, and optionally a quiet one when it passes, is canceled or waits on a block step.

- Company: Buildkite (https://www.justpush.io/recipes/buildkite)
- Event: `build.finished` (build.finished)
- Tags: CI/CD, Builds & CI
- Install: https://studio.justpush.io/recipes/buildkite/build-finished
- Web page: https://www.justpush.io/recipes/buildkite/build-finished

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. In Buildkite, go to Settings > Notification Services and click Add on Webhook.
3. Enter a description and paste your webhook URL (shown in Studio after install) as the Webhook URL.
4. Tick the build.finished event, choose which pipelines (and branches) to watch, and click Add Webhook Notification.
5. In **Settings → Notification Services**, add a **Webhook** with the URL above and tick **build.finished**. Pick the pipelines to watch, and optionally a branch filter such as `main` so only builds on that branch push.
6. Only failed builds push by default (High priority). Add `?all=1` to the URL to also get quiet pushes when a build passes or is canceled, and a Normal one when it stops at a block step waiting for approval:
7. ```
8. your webhook URL (shown in Studio after install)?all=1
9. ```
10. Want to know sooner? The **Build failing** recipe fires as soon as the first job fails, while the build is still running. If you tick both events on one webhook, a failed build pushes twice: once while failing and once when it finishes.
11. Saving the webhook sends a ping, which gives a quiet **🔔 Buildkite connected** push.
12. Buildkite sends your webhook token as `X-Buildkite-Token` (or an HMAC-SHA256 `X-Buildkite-Signature`), but JustPush doesn't check it yet, so keep your endpoint URL private.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// Buildkite → build.finished
// Fires when a Buildkite build finishes. Failures push by default; add ?all=1 to the webhook
// URL to also hear about passed and canceled builds and builds waiting on a block step.
// Docs: https://buildkite.com/docs/apis/webhooks/pipelines/build-events

// Final build states, how to describe them, and whether they push without ?all=1.
const STATES = {
    failed: { emoji: "❌", verb: "failed", priority: 1, always: true }, // broken build: needs you soon
    passed: { emoji: "✅", verb: "passed", priority: -1, always: false }, // good news, only with ?all=1
    canceled: { emoji: "⏹️", verb: "was canceled", priority: -1, always: false }, // on purpose, only with ?all=1
}
// Skipped and not_run builds never push.

// Look a key up in a table without tripping over inherited names like "toString".
function pick(table, key, fallback) {
    return typeof key === "string" && Object.prototype.hasOwnProperty.call(table, key) ? table[key] : fallback
}

// Header names can arrive in any case.
function header(request, name) {
    const headers = request.headers || {}
    const key = Object.keys(headers).find((k) => k.toLowerCase() === name)
    const value = key ? headers[key] : null
    return Array.isArray(value) ? value[0] : value
}

// Treat blank or non-string values as missing.
function text(value) {
    if (typeof value === "number" && Number.isFinite(value)) return String(value)
    return typeof value === "string" && value.trim() ? value.trim() : null
}

// Only link to real http(s) URLs.
function link(url, cta) {
    return typeof url === "string" && /^https?:\/\//.test(url) ? [{ cta, url }] : []
}

function handleRequest(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}
    // Buildkite sends X-Buildkite-Event, and the same name in the body's "event" field.
    const event = header(request, "x-buildkite-event") ?? (typeof body.event === "string" ? body.event : null)

    // Saving the webhook's settings sends a ping; confirm the connection quietly.
    if (event === "ping") {
        return {
            title: "🔔 Buildkite connected",
            message: `Webhook for ${text(body.organization?.name) ?? "your organization"} is working`,
            topic: "Buildkite",
            priority: -1,
        }
    }

    // Guard: only build.finished.
    const build = body.build
    if (event !== "build.finished" || !build || typeof build !== "object") {
        console.log(`Ignoring Buildkite "${event ?? "unknown"}" event`)
        return null
    }

    // ?all=1 on the webhook URL also sends the quieter pushes.
    const all = ["1", "true", "yes"].includes(String(request.query?.all ?? "").toLowerCase())

    // A build that stops at a block step sends build.finished with blocked: true, and again
    // when it really finishes. It's waiting for someone to unblock it.
    const state = build.blocked === true && build.state !== "failed"
        ? { emoji: "⏸️", verb: "is waiting for approval", priority: 0, always: false } // someone may need to act
        : pick(STATES, build.state, null)

    if (!state || (!state.always && !all)) {
        console.log(`Skipping build state "${build.state ?? "unknown"}"${build.blocked ? " (blocked)" : ""}`)
        return null
    }

    const pipeline = text(body.pipeline?.name) ?? "Pipeline"
    const number = text(build.number)
    const subject = (text(build.message) ?? "").split("\n")[0]
    const who = text(build.creator?.name)

    return {
        title: `${state.emoji} ${pipeline}${number ? ` #${number}` : ""} ${state.verb}`,
        message: (text(build.branch) ?? "A build") + (subject ? ` — ${subject}` : "") + (who ? ` (${who})` : ""),
        topic: "Buildkite",
        priority: state.priority,
        buttons: link(build.web_url, "View build"),
    }
}
```

## Buildkite build.finished webhook payload (sample)

```json
{
  "build": {
    "id": "f62a1b4d-10f9-4790-bc1c-e2c3a0c80983",
    "env": [],
    "url": "https://api.buildkite.com/v2/organizations/acme/pipelines/web/builds/1482",
    "state": "failed",
    "branch": "main",
    "commit": "abcd0b72a1e580e90712cdd9eb26d3fb41cd09c8",
    "number": 1482,
    "source": "webhook",
    "blocked": false,
    "creator": {
      "id": "3d3c3bf0-7d58-4afe-8fe7-b3017d5504de",
      "name": "Jane Doe",
      "email": "jane@example.com",
      "avatar_url": "https://www.gravatar.com/avatar/00000000000000000000000000000000",
      "created_at": "2025-05-22T12:36:45.309Z"
    },
    "message": "Fix checkout totals rounding\n\nCloses #88",
    "web_url": "https://buildkite.com/acme/web/builds/1482",
    "meta_data": [],
    "created_at": "2026-09-24T21:05:59.874Z",
    "failing_at": "2026-09-24T21:07:31.204Z",
    "graphql_id": "QnVpbGQtLS1mNjJhMWI0ZC0xMGY5LTQ3OTAtYmMxYy1lMmMzYTBjODA5ODM=",
    "started_at": "2026-09-24T21:06:10.112Z",
    "finished_at": "2026-09-24T21:08:59.874Z",
    "pull_request": null,
    "scheduled_at": "2026-09-24T21:05:59.874Z",
    "cancel_reason": null
  },
  "event": "build.finished",
  "sender": {
    "id": "8a7693f8-dbae-4783-9137-84090fce9045",
    "name": "Jane Doe"
  },
  "pipeline": {
    "id": "849411f9-9e6d-4739-a0d8-e247088e9b52",
    "url": "https://api.buildkite.com/v2/organizations/acme/pipelines/web",
    "name": "Web",
    "slug": "web",
    "web_url": "https://buildkite.com/acme/web",
    "repository": "git@github.com:acme/web.git",
    "default_branch": "main"
  }
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into Buildkite and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
