# Run SEO checks in CI

> Set reproducible Sonjoo score, rule, and crawl coverage gates and keep the JSON report available when a check fails.

Canonical documentation: <https://sajaddehshiri.ir/sonjoo-seo-audit/guides/ci-gates/>

Run a bounded Sonjoo audit, keep the JSON report as an artifact, and propagate the CLI exit code to the job. Pin the Sonjoo version and commit the configuration used by the job so you can compare runs.

## Gate on a minimum score or page coverage

Add a score threshold with `--fail-under`. For crawls, use `--min-coverage` to require a minimum percentage of measured pages among attempted pages. The gate uses a whole percentage rounded down. At the default threshold of 70, a score of exactly 70 meets the score gate. Coverage is checked when a positive threshold and measured and attempted counts are available.

```sh
sonjoo audit https://example.com/ --crawl --max-pages 25 --max-duration 120 \
  --fail-under 80 --min-coverage 90 --no-save --format json --output audit.json
```

Choose thresholds based on the scope you actually measure. Inspect [page and rule coverage](https://sajaddehshiri.ir/sonjoo-seo-audit/guides/reports-and-coverage/#what-does-coverage-mean) before raising a threshold.

The [local fixture](https://sajaddehshiri.ir/sonjoo-seo-audit/start/first-audit/#read-the-title-finding) has a crawl branch with three pages. This example requires Sonjoo to measure every attempted page and writes the coverage evidence to JSON.


```sh
target="${SONJOO_DOCS_TARGET:-http://127.0.0.1:18765}"
sonjoo audit "$target/crawl-branch/" --crawl --max-pages 3 --categories core \
  --fail-under 0 --min-coverage 100 --no-save --format json --output coverage-audit.json
```

Expect exit 0 and `gate.coverage` with `measured: 3`, `attempted: 3`, and `passed: true`.

## Require a specific rule

To reproduce a pass and a failure, start the [local fixture](https://sajaddehshiri.ir/sonjoo-seo-audit/start/first-audit/#read-the-title-finding). Run the example from an empty directory.


```sh
target="${SONJOO_DOCS_TARGET:-http://127.0.0.1:18765}"
cat > ci-audit.toml <<'TOML'
[rules]
enable = ["core-title-present"]

[rules.override]
core-title-present = { fail_ci = true }

[ci]
fail_under = 0
TOML

sonjoo audit "$target/healthy" --config ci-audit.toml --no-save \
  --format json --output passing-audit.json

status=0
sonjoo audit "$target/healthy?remove-title=1" --config ci-audit.toml --no-save \
  --format json --output failing-audit.json || status=$?
printf 'Audit exit code: %s\n' "$status"
(exit "$status")
```

The first command exits 0. The second exits 1 and keeps `failing-audit.json`. Inspect `gate.failedCiRules` to see the failed rule and its URL. Required checks that could not run can also fail the gate. Inspect `gate.unevaluated` for the reason. Sonjoo rejects configuration that disables a required rule or excludes its category.

## Keep the report when the job fails

Use a fresh output path for each run. Configure your CI system to upload the report after the audit step even when that step fails, then preserve the original exit status. Treat an error envelope as an execution error. Save the diagnostic separately if you also capture standard error. [Exit code reference](https://sajaddehshiri.ir/sonjoo-seo-audit/cli/exit-codes/) · [JSON report shapes](https://sajaddehshiri.ir/sonjoo-seo-audit/cli/exports/)
