---
title: "Azure Pipelines"
description: "Gate an Azure DevOps pipeline on a TRUSTIVAN policy with the trustivan CLI and publish the findings as a CodeAnalysisLogs SARIF artifact."
---

There is no Azure DevOps extension from TRUSTIVAN. The recipe downloads the
[`trustivan`](/cli-reference/trustivan) CLI, a single static binary, and runs
`trustivan gate` in a script step. 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 **secret** pipeline variable
  (or a variable group) 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 such as Azure Container Registry needs a
  credential stored in TRUSTIVAN first. See
  [Scan a private registry image](/how-to/scan-a-private-registry-image).

## The pipeline

```yaml azure-pipelines.yml
trigger:
  branches:
    include:
      - main

pool:
  vmImage: ubuntu-latest

variables:
  TRUSTIVAN_URL: https://app.trustivan.com
  IMAGE: myregistry.azurecr.io/acme/api:$(Build.SourceVersion)

steps:
  # Your build-and-push steps go first; the gate needs the pushed image.

  - script: |
      set -eu
      base=https://github.com/trustivan/trustivan/releases/latest/download
      curl -fsSLO "$base/trustivan-linux-amd64"
      curl -fsSLO "$base/SHA256SUMS"
      grep ' trustivan-linux-amd64$' SHA256SUMS | sha256sum -c -
      chmod +x trustivan-linux-amd64
    displayName: Install the TRUSTIVAN CLI

  - script: >-
      ./trustivan-linux-amd64 gate
      --image "$(IMAGE)"
      --idempotency-key "$(Build.BuildId)"
      --sarif "$(Build.ArtifactStagingDirectory)/trustivan.sarif"
    displayName: Gate on the TRUSTIVAN policy
    env:
      TRUSTIVAN_API_KEY: $(TRUSTIVAN_API_KEY)

  - task: PublishBuildArtifacts@1
    displayName: Publish the SARIF log
    condition: succeededOrFailed()
    inputs:
      PathtoPublish: $(Build.ArtifactStagingDirectory)/trustivan.sarif
      ArtifactName: CodeAnalysisLogs
```

The `env:` mapping is required: Azure Pipelines does not put a secret variable
into a script's environment unless the step names it. `TRUSTIVAN_URL` is not a
secret and reaches the script without one. `$(Build.BuildId)` is the
idempotency key, so a re-run of a failed job reuses its scan instead of queueing
a second one. To pin a CLI release, replace `latest/download` with
`download/<tag>`.

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

Azure Pipelines 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`.

## 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 `CodeAnalysisLogs` artifact** holds `trustivan.sarif`. That artifact
  name is the one Microsoft's **SARIF SAST Scans Tab** extension reads, so with
  that extension installed the findings appear on a **Scans** tab of the run.
  The file is written before the verdict is acted on, so it exists for a
  blocked build too.
- **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
  - script: ./trustivan-linux-amd64 gate --image "$(IMAGE)" || test $? -eq 1
    displayName: Gate on the TRUSTIVAN policy (report only)
    env:
      TRUSTIVAN_API_KEY: $(TRUSTIVAN_API_KEY)
```

## Troubleshooting

| Symptom | Cause |
|---|---|
| `TRUSTIVAN_API_KEY is not set` | The step has no `env:` mapping for the secret, or the variable group is not linked |
| `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 the image. For ACR, check the registry credential stored in TRUSTIVAN |
| The publish step fails with no file | The scan failed, so no SARIF was written. The gate step's log says why |

## 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`
- [Findings on pull requests with SARIF](/integrations/sarif-code-scanning)
