How to Validate OpenAPI Specs in GitHub Actions
A drifted OpenAPI spec silently breaks generated clients and docs; CI validation keeps the contract honest.
Run a linter such as @stoplight/spectral-cli or @redocly/cli lint against your spec file so style and structural errors fail the job.
Steps
- Add a ruleset (e.g.
.spectral.yaml) extendingspectral:oas. - Install the linter in CI.
- Run it against your
openapi.yaml. - Let a non-zero exit fail the pull request.
Workflow
.github/workflows/openapi.yml
name: Validate OpenAPI
on:
pull_request:
paths: ['openapi.yaml', '.spectral.yaml']
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20' }
- run: npx @stoplight/spectral-cli lint openapi.yaml --fail-severity=warnNotes
- Scope the trigger with
pathsso it only runs when the spec or ruleset changes. - Use
--fail-severity=warnto treat warnings as failures and keep the spec clean.