CI gate
A GitHub Actions job that fails a pull request on a breaking contract change, a lint rule at error or generated code that is out of date, and uploads the findings as SARIF to code scanning.
Three checks keep a contract honest on every pull request, and each one is a contractdiff command whose exit code is the verdict:
| check | command | fails when |
|---|---|---|
| breaking changes | contractdiff diff base.yaml openapi.yaml | a change breaks a caller built on the base |
| form | contractdiff lint | a rule set to error finds something |
| generated code | contractdiff gen --check | a generated file is stale or was edited by hand |
The config
openapi: 3.1.0
info:
title: Pets
version: 1.0.0
paths:
/pets/{id}:
get:
operationId: getPet
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
"200":
description: A pet
content:
application/json:
schema:
type: object
required: [name]
properties:
name:
type: string{
"input": "openapi.yaml",
"targets": { "types": { "generator": "types", "out": "src/api" } },
"lint": {
"extends": ["recommended"],
"rules": { "operation-tags": "error", "info-description": "off" },
"warnFor": "0d"
},
"diff": { "decisions": "error" }
}operation-tags is at error, and this contract has no tags, so the lint step fails:
openapi.yaml
error operation-tags #/paths/~1pets~1{id}/get `tags` is missing
fix: add `tags` naming the resource the operation belongs to
warn operation-description #/paths/~1pets~1{id}/get has no description or summary
fix: add a `description` saying what the operation does and when to call it
warn parameter-description #/paths/~1pets~1{id}/get/parameters/0 id: `description` is missing
fix: add a `description` saying what the parameter does and what values it takes
1 error(s), 2 warning(s). Checked 10 rule(s) on 1 input(s), 12 node(s); not carried: none.
recommended: 75% (9 of 12 checks passed)The generated code step, after a gen that was committed:
write src/api/types.ts
write contractdiff.map.jsonup to date: 2 file(s)diff.decisions: "error" makes a breaking change without an x-change fail the run as well; see
Decisions. Leave it out to keep the default, warn.
The workflow
diff needs the contract as it is on the base branch. In 1.0.0 it reads files only, so the job
writes the base version out with git show first. The checkout needs the base branch's history
for that, hence fetch-depth: 0.
name: contract
on: pull_request
permissions:
contents: read
security-events: write
jobs:
contract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0
- uses: actions/setup-node@v5
with:
node-version: 24
- run: npm ci
- run: git show origin/${{ github.base_ref }}:openapi.yaml > base.yaml
- name: Breaking changes
run: npx contractdiff diff base.yaml openapi.yaml
- name: Breaking changes as SARIF
if: always()
run: npx contractdiff diff base.yaml openapi.yaml --sarif > contract.sarif || true
- uses: github/codeql-action/upload-sarif@v4
if: always()
with:
sarif_file: contract.sarif
category: contractdiff
- name: Lint
run: npx contractdiff lint --since merge-base
- name: Generated code is current
run: npx contractdiff gen --checkNotes on the steps:
- The SARIF step prints SARIF instead of text, so it runs separately from the gating step; its exit code is ignored because the gating step already reported it. Each finding lands on the contract file in the pull request's code scanning view.
lint --since merge-baselints in full only the operations this pull request added or changed. Drop--sinceonce the contract is clean.- A planned major. A pull request that intends a breaking change still fails the
diffstep, by design. Write thex-changeand move the major version; then either rundiffwith--changeset <package>so the release is recorded, or let a reviewer approve the red step. - Exit codes.
1is a finding.2is a usage error and3a config that did not load; both mean the job is wrong, not the contract.
Releases that stay in the field
A team that supports more than the last release keeps the published contracts in a folder and checks the new one against all of them:
npx contractdiff diff releases/ openapi.yaml --transitiveThe output names the oldest release each finding breaks. See Breaking changes.
Coming from Optic
Move from Optic's diff, lint and changelog to contractdiff: diff for breaking changes, lint with forwards-only adoption through --since, the changelog as diff output, and what Optic did that contractdiff does not, such as capturing traffic into a spec.
Agents
How a coding agent uses contractdiff: explain one operation resolved and size-capped instead of reading the whole spec, lint a contract or an MCP tool list for agent-readiness, and drive every command through --json, --schema and the --mcp server.