---
title: "CI pipelines"
description: "Scan an image and gate a build from any CI system using curl and a scoped API key, with GitHub Actions and GitLab CI examples."
---

Nothing needs to be installed. A gate is one shell script: request a scan,
poll it to a terminal state, ask for a verdict, and branch on the body. The
[`trustivan gate`](/cli-reference/trustivan) command is the same loop as a
single binary, if you would rather not maintain the script.

## The credential

Issue one key per pipeline, with the minimum scopes:

| Scope | Why |
|---|---|
| `scans:create` | queue the scan |
| `scans:read` | poll it |
| `verdicts:request` | ask whether it passes |

Nothing else. In particular **not** `findings:suppress`, because a runner that
can queue scans has no business being able to silence its own failures. And
`policies:manage` is not grantable to a key at all, so the pipeline cannot
rewrite the policy that judges it. Someone with a browser session writes the
policy; see
[Gate a pipeline on a policy](/tutorials/gate-a-pipeline).

Store the secret in your CI provider's secret store, never in the repository,
never in the job definition. It is shown exactly once at creation and expires
in at most 365 days, so put the rotation in your calendar
([Rotate an API key](/how-to/rotate-an-api-key)).

## The generic recipe

Works anywhere with `bash`, `curl` and `jq`.

```bash
#!/usr/bin/env bash
set -euo pipefail

: "${TRUSTIVAN_URL:?}" "${TRUSTIVAN_KEY:?}" "${IMAGE:?}" "${BUILD_ID:?}"

auth=(-H "Authorization: Bearer $TRUSTIVAN_KEY")
POLL_TIMEOUT=${POLL_TIMEOUT:-900}   # seconds; a bounded wait, not an open one

# 1. Queue the scan. The build ID makes the retry safe.
scan=$(curl -sf -X POST "$TRUSTIVAN_URL/api/v1/scans" "${auth[@]}" \
  -H 'Content-Type: application/json' \
  -H "Idempotency-Key: $BUILD_ID" \
  -d "$(jq -nc --arg image "$IMAGE" '{image: $image}')")
scan_id=$(jq -r '.scan.id' <<<"$scan")

# 2. Poll to a terminal state.
deadline=$(( $(date +%s) + POLL_TIMEOUT ))
while :; do
  scan=$(curl -sf "$TRUSTIVAN_URL/api/v1/scans/$scan_id" "${auth[@]}")
  case "$(jq -r '.scan.status' <<<"$scan")" in
    succeeded) break ;;
    failed)
      echo "scan failed: $(jq -r '.scan.error // "unknown"' <<<"$scan")" >&2
      exit 2 ;;
  esac
  [ "$(date +%s)" -lt "$deadline" ] || { echo "scan timed out" >&2; exit 2; }
  sleep 5
done

echo "advisory database: $(jq -r '.scan.database_state' <<<"$scan")"

# 3. Ask for the verdict, capturing the status and the body separately.
status=$(curl -s -o /tmp/verdict.json -w '%{http_code}' \
  -X POST "$TRUSTIVAN_URL/api/v1/scans/$scan_id/verdict" "${auth[@]}")

# 4. Transport failure is NOT a policy failure.
if [ "$status" -lt 200 ] || [ "$status" -ge 300 ]; then
  echo "TRUSTIVAN did not answer (HTTP $status)" >&2
  exit 2
fi

# 5. The result is in the body.
echo "TRUSTIVAN: $(jq -r '.result' /tmp/verdict.json) — $(jq -r '.summary' /tmp/verdict.json)"
jq -r '.rules[] | select(.fired) | "  \(.effect): \(.description) (\(.matched) matched)"' \
  /tmp/verdict.json

[ "$(jq -r '.blocking' /tmp/verdict.json)" = "true" ] && exit 1
exit 0
```

Three exit codes, because there are three outcomes: `0` ship, `1` the policy
says stop, `2` TRUSTIVAN could not answer. Collapsing the last two is the
mistake this whole design is arranged to prevent.

## The four things that matter

**Branch on the body, never the status line.** `POST /scans/{id}/verdict`
returns `201` when it records a decision and `200` when it replays one; a
**failing verdict is still a successful request**. A pipeline that treated a
policy failure as an HTTP error could not distinguish "the build should stop"
from "TRUSTIVAN is unreachable", and those demand opposite reactions: the first
fails the build, the second is retried or escalated, and neither is silently
treated as a pass. `blocking` is precomputed in the body so your gate does not
reimplement TRUSTIVAN's opinion about which results stop a deployment.

**Give the key the minimum scopes**, and store it as a secret. See the table
above.

**Set a real poll timeout.** A large image takes minutes to pull and unpack. An
unbounded loop is a build that hangs rather than one that reports a problem;
`exit 2` on timeout puts it in the same bucket as any other infrastructure
failure.

**Use the build ID as the idempotency key.** It is reserved before any work, so
two concurrent retries cannot both queue a scan, and a replayed request is
marked with `Idempotent-Replay: true`.

## GitHub Actions

```yaml
name: security-gate

on:
  push:
    branches: [main]

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # Build and push the image first: TRUSTIVAN pulls it from the registry,
      # not from this runner. It never touches a Docker socket or a build host.
      - name: Gate on the TRUSTIVAN policy
        env:
          TRUSTIVAN_URL: https://app.trustivan.com
          TRUSTIVAN_KEY: ${{ secrets.TRUSTIVAN_KEY }}
          IMAGE: ghcr.io/${{ github.repository }}:${{ github.sha }}
          BUILD_ID: ${{ github.run_id }}-${{ github.run_attempt }}
        run: ./ci/trustivan-gate.sh
```

`github.run_id` alone would be reused by a re-run, so the attempt number is
appended: a deliberate re-run should be able to queue a fresh scan, while a
transient retry inside one attempt should not.

TRUSTIVAN does not scan pull requests and is not triggered by a push. This job
is what runs on push, and it is the whole of the GitHub story. See
[Integrations overview](/integrations/overview).

## GitLab CI

```yaml
security-gate:
  stage: test
  image: alpine:3.21
  variables:
    TRUSTIVAN_URL: https://app.trustivan.com
    IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    BUILD_ID: $CI_PIPELINE_ID-$CI_JOB_ID
  before_script:
    - apk add --no-cache bash curl jq
  script:
    - ./ci/trustivan-gate.sh
```

`TRUSTIVAN_KEY` goes in a masked, protected CI/CD variable rather than in this
file.

To let a failing policy report without blocking while a team settles in, use
`allow_failure` on the job rather than making the script swallow the exit code.
The distinction between exit `1` and exit `2` is worth keeping.

## Other CI systems

Each of these runs `trustivan gate` with the system's own secret store, artifact
upload and retry semantics: [Jenkins](/integrations/jenkins),
[CircleCI](/integrations/circleci), [Azure Pipelines](/integrations/azure-pipelines),
[Bitbucket Pipelines](/integrations/bitbucket-pipelines) and
[Buildkite](/integrations/buildkite).

## Certificate checks with tls-check

`tls-check` fails a build when a host's certificate is expired, expiring,
untrusted, mismatched or weak, or the host still accepts TLS 1.0/1.1, and with
`-file` it judges a PEM file, a Kubernetes TLS Secret or a cert-manager
Certificate before it is deployed. It needs no TRUSTIVAN account. On GitHub it is
an action ([Certificates](/configuration/certificates)); there is no release
binary for it, so anywhere else you build it from the repository with Go:

```bash
set -eu
git clone --depth 1 https://github.com/trustivan/trustivan.git trustivan-src
(cd trustivan-src/backend &&
  GOEXPERIMENT=jsonv2 CGO_ENABLED=0 go build -trimpath -o ../../tls-check ./cmd/tls-check)

./tls-check -fail-on medium shop.example.com api.example.com:8443
./tls-check -max-validity-days 90 -names shop.example.com -file 'deploy/k8s/*.yaml'
```

The Go version is the one in `backend/go.mod`. Exit `0` means nothing at or
above `-fail-on` (`low`, `medium`, `high` or `never`), `1` means a host or file
failed, and `2` is a usage error.

`-upload` also posts the result to TRUSTIVAN as pipeline gate evidence on the
Certificates page, with `TRUSTIVAN_API_KEY` (scope `scans:create`) and
`TRUSTIVAN_URL` from the environment. It reads the repository, ref, commit and
run link from GitHub Actions or GitLab CI variables only. On any other system,
set the GitLab names yourself so the evidence says where it came from:

```bash
CI_PROJECT_PATH=acme/shop CI_COMMIT_REF_NAME="$BRANCH" CI_COMMIT_SHA="$COMMIT" \
CI_JOB_URL="$BUILD_URL" ./tls-check -upload -file 'deploy/k8s/*.yaml'
```

## Gotchas

- **The image must already be pushed** and reachable from TRUSTIVAN. A public
  registry needs nothing; a private one needs a credential stored against
  that registry host beforehand (Pro and Enterprise). Either way TRUSTIVAN
  never reads from the runner: it pulls the image itself, so a credential
  in your CI environment is not one TRUSTIVAN can see or use.
- **`409` on the verdict request means no policy is enabled** for the
  organisation. That is a configuration problem, not a security decision.
  Treat it as exit `2`.
- **Check `database_state`.** A scan against stale advisory data reports "no
  vulnerabilities" with exactly the same confidence as a correct one.
- **A retried verdict request returns the same decision**, not a second one:
  idempotency here is a unique constraint on `(scan, policy version)`.
- **Prefer a webhook to a long poll for fleets.** For one image per build,
  polling is fine; across many, subscribe to `verdict.recorded` instead
  ([Webhooks](/integrations/webhooks)).

## See also

- [Gate a pipeline on a policy](/tutorials/gate-a-pipeline): the same
  flow end to end, including writing the policy.
- [Request a scan](/how-to/request-a-scan): the scan half in reference
  form.
- [Scopes](/api-reference/scopes): what to grant, and what cannot be
  granted.
