---
title: "CircleCI"
description: "Gate a CircleCI workflow on a TRUSTIVAN policy with the trustivan CLI, store the findings as a SARIF artifact, and keep the key in a context."
---

There is no CircleCI orb. The recipe downloads the
[`trustivan`](/cli-reference/trustivan) CLI, a single static binary, and runs
`trustivan gate`. 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. Put it in a CircleCI **context** named
  `trustivan` as the environment variable `TRUSTIVAN_API_KEY`, and restrict the
  context to the security group that may use it. 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 config

```yaml .circleci/config.yml
version: 2.1

jobs:
  trustivan-gate:
    docker:
      - image: cimg/base:current
    environment:
      TRUSTIVAN_URL: https://app.trustivan.com
    steps:
      - run:
          name: Install the TRUSTIVAN CLI
          command: |
            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
      - run:
          name: Gate on the TRUSTIVAN policy
          command: |
            ./trustivan-linux-amd64 gate \
              --image "registry.example.com/acme/api:${CIRCLE_SHA1}" \
              --idempotency-key "${CIRCLE_WORKFLOW_ID}" \
              --sarif trustivan.sarif
      - store_artifacts:
          path: trustivan.sarif
          when: always

workflows:
  build:
    jobs:
      # Your build-and-push job goes first; the gate needs the pushed image.
      - trustivan-gate:
          context: trustivan
```

`CIRCLE_WORKFLOW_ID` is the idempotency key, so a retried request inside one
workflow reuses its scan instead of queueing a second one. `when: 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>`. On an `arm`
resource class, use `trustivan-linux-arm64` in all four places.

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

CircleCI fails the job 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 output** has the verdict, the policy and version, the scan id and
  every rule, with `!` beside the ones that fired.
- **The job'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 job:

```yaml
      - run:
          name: Gate on the TRUSTIVAN policy (report only)
          command: ./trustivan-linux-amd64 gate --image "registry.example.com/acme/api:${CIRCLE_SHA1}" || test $? -eq 1
```

## Troubleshooting

| Symptom | Cause |
|---|---|
| `TRUSTIVAN_API_KEY is not set` | The job is not attached to the `trustivan` context, or the person who triggered it is not in the context's security group |
| `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 |
| `scan <id> was still <status> after 20m0s` | Raise `--timeout`, or check the scan in the dashboard |

## 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`
- [Jenkins](/integrations/jenkins), [Azure Pipelines](/integrations/azure-pipelines),
  [Bitbucket Pipelines](/integrations/bitbucket-pipelines),
  [Buildkite](/integrations/buildkite)
