# GitLab deployment notifications on your phone

> Get a push notification when a GitLab deployment succeeds or fails, with an urgent push when production fails. Filter by environment if you like.

- Company: GitLab (https://www.justpush.io/recipes/gitlab)
- Event: `Deployment Hook` (Deployment events)
- Tags: Code hosting, Deployments
- Install: https://studio.justpush.io/recipes/gitlab/deployment
- Web page: https://www.justpush.io/recipes/gitlab/deployment

## 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 your GitLab project (or group), go to Settings > Webhooks and click Add new webhook.
3. Paste your webhook URL (shown in Studio after install) as the URL.
4. Under Trigger, tick Deployment events and click Add webhook. Use the Test dropdown to send a sample event.
5. Tick **Deployment events** under **Trigger**. Every successful or failed deployment pushes; running, canceled, blocked and approval events are ignored. Options go on the URL:
6. `?env=production` only pushes for one environment (matches its name, slug or tier).
7. `?failed=1` skips successful deployments.
8. ```
9. your webhook URL (shown in Studio after install)?env=production&failed=1
10. ```
11. A failed deploy to a **production**-tier environment is Urgent. When someone rejects a deployment that needs approval, GitLab also sends a `failed` event for it, so a rejection gives a failure push too.
12. GitLab can sign deliveries with a **Signing token** (`webhook-signature`, HMAC-SHA256) or send a plain **Secret token** (`X-Gitlab-Token`), but JustPush doesn't check either 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
// GitLab → Deployment Hook (deployment finished)
// Fires when a deployment succeeds or fails. Add ?env=production to the URL to only hear about one
// environment, and ?failed=1 to skip successful deployments.
// Docs: https://docs.gitlab.com/user/project/integrations/webhook_events/#deployment-events

// 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
}

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

// Query flags such as ?failed=1.
function flag(request, name) {
    return ["1", "true", "yes"].includes(String(request.query?.[name] ?? "").toLowerCase())
}

function handleRequest(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}
    const event = header(request, "x-gitlab-event")

    // Guard: only deployment events. The header decides when it's there; the payload's
    // shape is the fallback (for example in Studio's Test runner).
    if (event ? event !== "Deployment Hook" : body.object_kind !== "deployment") {
        console.log(`Ignoring GitLab "${event ?? body.object_kind ?? "unknown"}" event`)
        return null
    }

    // Only finished deployments; running, canceled, blocked and approval events are skipped.
    const failed = body.status === "failed"
    if (!failed && (body.status !== "success" || flag(request, "failed"))) {
        console.log(`Skipping deployment status "${body.status ?? "unknown"}"`)
        return null
    }

    // ?env=production matches the environment's name, slug or tier.
    const only = typeof request.query?.env === "string" ? request.query.env.trim().toLowerCase() : ""
    const names = [body.environment, body.environment_slug, body.environment_tier]
    if (only && !names.some((value) => typeof value === "string" && value.toLowerCase() === only)) {
        console.log(`Skipping deployment to "${body.environment ?? "unknown"}"`)
        return null
    }

    const env = body.environment ?? "an environment"
    const production = body.environment_tier === "production"
    const repo = body.project?.path_with_namespace ?? body.project?.name ?? "a project"
    const who = body.user?.username

    return {
        title: failed ? `❌ Deploy to ${env} failed` : `✅ Deployed to ${env}`,
        message:
            [repo, body.short_sha].filter(Boolean).join(" · ") +
            (body.commit_title ? ` — ${body.commit_title}` : "") +
            (who ? ` (@${who})` : ""),
        topic: "GitLab",
        // Urgent when production is broken, High for other failed deploys,
        // Normal for a production release, Low for other successful deploys.
        priority: failed ? (production ? 2 : 1) : production ? 0 : -1,
        buttons: [
            ...link(body.deployable_url, "View job"),
            ...(failed ? [] : link(body.environment_external_url, "Open site")),
        ],
    }
}
```

## GitLab Deployment Hook webhook payload (sample)

```json
{
  "user": {
    "id": 42,
    "name": "Jane Doe",
    "email": "[REDACTED]",
    "username": "janedoe"
  },
  "status": "failed",
  "project": {
    "id": 7,
    "name": "web",
    "web_url": "https://gitlab.example.com/acme/web",
    "namespace": "Acme",
    "default_branch": "main",
    "path_with_namespace": "acme/web"
  },
  "user_url": "https://gitlab.example.com/janedoe",
  "short_sha": "279484c0",
  "commit_url": "https://gitlab.example.com/acme/web/-/commit/279484c09fbe69ededfced8c1bb6e6d24616b468",
  "environment": "production",
  "object_kind": "deployment",
  "commit_title": "Fix checkout totals rounding",
  "deployable_id": 5530,
  "deployment_id": 915,
  "deployable_url": "https://gitlab.example.com/acme/web/-/jobs/5530",
  "environment_slug": "production",
  "environment_tier": "production",
  "status_changed_at": "2026-09-28T14:02:10.000+02:00",
  "environment_external_url": "https://shop.example.com"
}
```

## 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 GitLab 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.
