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

There is no Jenkins plugin. The recipe downloads the
[`trustivan`](/cli-reference/trustivan) CLI, a single static binary, and runs
`trustivan gate` in a `sh` 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. Store it as a Jenkins **Secret text**
  credential with the id `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 and never reads the agent's Docker daemon. A private registry
  needs a credential stored in TRUSTIVAN first; see
  [Scan a private registry image](/how-to/scan-a-private-registry-image).
- **A Linux agent** with `curl` and `sha256sum`, and outbound HTTPS to
  `github.com` (the download) and to your TRUSTIVAN deployment.

## The Jenkinsfile

```groovy Jenkinsfile
pipeline {
  agent any

  environment {
    TRUSTIVAN_URL = 'https://app.trustivan.com'
    IMAGE         = "registry.example.com/acme/api:${env.GIT_COMMIT}"
  }

  stages {
    stage('TRUSTIVAN policy gate') {
      steps {
        sh '''
          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
        '''
        withCredentials([string(credentialsId: 'trustivan-api-key', variable: 'TRUSTIVAN_API_KEY')]) {
          sh './trustivan-linux-amd64 gate --image "$IMAGE" --idempotency-key "$BUILD_TAG" --sarif trustivan.sarif'
        }
      }
      post {
        always {
          archiveArtifacts artifacts: 'trustivan.sarif', allowEmptyArchive: true
        }
      }
    }
  }
}
```

The `sh` strings are single-quoted on purpose: the shell expands
`$TRUSTIVAN_API_KEY`, not Groovy, so the key is never interpolated into the
pipeline script. `$BUILD_TAG` is the idempotency key, so a retried request
inside one build reuses its scan instead of queueing a second one. To pin a CLI
release, replace `latest/download` with `download/<tag>`.

On an `arm64` agent, use `trustivan-linux-arm64` in all four places.

## What it gates on

| Exit code | Meaning | Jenkins shows |
|---|---|---|
| `0` | The policy passed | Stage succeeds |
| `1` | The policy blocked the build | Stage fails; the 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 | Stage fails; the log line starts `trustivan:` and names the cause |

A failed scan is never treated as a pass. The default wait is 20 minutes; change
it with `--timeout 30m`.

## See the results

- **The console log** has the verdict, the policy and version, the scan id and
  every rule, with `!` beside the ones that fired.
- **`trustivan.sarif`** is archived on the build page. It is written before the
  verdict is acted on, so it exists for a blocked build too. With the Warnings
  Next Generation plugin installed, `recordIssues tool: sarif(pattern: 'trustivan.sarif')`
  in the `post` block shows the findings as a build report.
- **The dashboard** has the scan and its findings under the asset.

## Report without blocking

To let a new policy report before it is enforced, accept exit `1` and nothing
else:

```groovy
sh './trustivan-linux-amd64 gate --image "$IMAGE" --idempotency-key "$BUILD_TAG" || test $? -eq 1'
```

Exit `2` still fails the stage, so an outage is never read as a pass.

## Troubleshooting

| Symptom | Cause |
|---|---|
| `TRUSTIVAN_API_KEY is not set` | The `withCredentials` block is missing, or the credential id does not match |
| `sha256sum: WARNING: 1 computed checksum did NOT match` | The download was cut short or altered. Do not run the file |
| `... (401)` | The key is wrong, revoked or expired. See [Rotate an API key](/how-to/rotate-an-api-key) |
| `... (403)` | The key lacks a scope; `findings:read` is the usual one when `--sarif` is set |
| `... /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` | The scan outlived `--timeout`. Raise it, 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`
- [Findings on pull requests with SARIF](/integrations/sarif-code-scanning)
