---
title: "Bitbucket Pipelines"
description: "Gate a Bitbucket pipeline on a TRUSTIVAN policy with the trustivan CLI, keep the SARIF findings as a step artifact, and tell a block from an outage."
---

There is no Bitbucket pipe from TRUSTIVAN. The recipe downloads the
[`trustivan`](/cli-reference/trustivan) CLI, a single static binary, and runs
`trustivan gate` in the step's script. The gate requests a scan, waits for it,
asks for a verdict and exits `1` when the policy blocks the build.

## Prerequisites

- **An API key** with `scans:create`, `scans:read` and `verdicts:request`, plus
  `findings:read` for the SARIF file. Add it as a **secured** repository or
  deployment variable named `TRUSTIVAN_API_KEY`. See [Scopes](/api-reference/scopes).
- **An enabled policy** in the organization, or a policy id to pass with
  `--policy`. Without one the verdict request answers `409`. See
  [Gate a pipeline on a policy](/tutorials/gate-a-pipeline).
- **The image already pushed** to a registry TRUSTIVAN can pull from. TRUSTIVAN
  pulls it itself; a private registry needs a credential stored in TRUSTIVAN
  first. See [Scan a private registry image](/how-to/scan-a-private-registry-image).

## The pipeline

```yaml bitbucket-pipelines.yml
image: atlassian/default-image:4

pipelines:
  branches:
    main:
      # Your build-and-push step goes first; the gate needs the pushed image.
      - step:
          name: TRUSTIVAN policy gate
          script:
            - export TRUSTIVAN_URL=https://app.trustivan.com
            - curl -fsSLO https://github.com/trustivan/trustivan/releases/latest/download/trustivan-linux-amd64
            - curl -fsSLO https://github.com/trustivan/trustivan/releases/latest/download/SHA256SUMS
            - grep ' trustivan-linux-amd64$' SHA256SUMS | sha256sum -c -
            - chmod +x trustivan-linux-amd64
            - >-
              ./trustivan-linux-amd64 gate
              --image "registry.example.com/acme/api:$BITBUCKET_COMMIT"
              --idempotency-key "$BITBUCKET_PIPELINE_UUID"
              --sarif trustivan.sarif
          artifacts:
            upload:
              - name: trustivan-sarif
                paths:
                  - trustivan.sarif
                capture-on: always
```

Bitbucket masks a secured variable in the log and passes it to the script as
`$TRUSTIVAN_API_KEY`, which is where the CLI reads it. `$BITBUCKET_PIPELINE_UUID`
is the idempotency key, so re-running a failed step reuses its scan instead of
queueing a second one. `capture-on: always` keeps the SARIF file on a blocked
build, which is the build it matters for. To pin a CLI release, replace
`latest/download` with `download/<tag>` in both URLs.

## What it gates on

| Exit code | Meaning |
|---|---|
| `0` | The policy passed |
| `1` | The policy blocked the build. The step log shows `TRUSTIVAN policy gate: FAIL` and the rules that fired |
| `2` | TRUSTIVAN could not answer: missing key, network, timeout, a failed scan, no policy. The log line starts `trustivan:` and names the cause |

Bitbucket fails the step on both. A failed scan is never treated as a pass. The
default wait is 20 minutes; change it with `--timeout 30m`, and keep it below
the step's `max-time`.

## See the results

- **The step log** has the verdict, the policy and version, the scan id and
  every rule, with `!` beside the ones that fired.
- **The step's Artifacts tab** has `trustivan.sarif`, which any SARIF 2.1.0
  viewer opens.
- **The dashboard** has the scan and its findings under the asset.

## Report without blocking

Accept exit `1` and nothing else, so an outage still fails the step:

```yaml
            - ./trustivan-linux-amd64 gate --image "registry.example.com/acme/api:$BITBUCKET_COMMIT" || test $? -eq 1
```

## Troubleshooting

| Symptom | Cause |
|---|---|
| `TRUSTIVAN_API_KEY is not set` | The variable is missing, or it is a deployment variable and the step has no `deployment:` |
| `sha256sum: WARNING: 1 computed checksum did NOT match` | The download was cut short or altered. Do not run the file |
| `... (401)` / `... (403)` | The key is wrong or expired, or lacks a scope (`findings:read` with `--sarif`). See [Rotate an API key](/how-to/rotate-an-api-key) |
| `... /verdict: ... (409)` | No policy is enabled for the organization |
| `the scan failed (...)` | TRUSTIVAN could not pull or read the image. Check the reference and the registry credential |
| The step stops at its time limit | The scan is slower than the step's `max-time`; lower `--timeout` so the gate reports exit `2` first |

## See also

- [`trustivan` CLI reference](/cli-reference/trustivan): every flag
- [CI pipelines](/integrations/ci-pipelines): the same gate with `curl`, and a
  certificate check with `tls-check`
- [Jira](/integrations/jira): open an issue for a blocking verdict
