diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c6cb43f..f2c9ab3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,4 +40,4 @@ jobs: BASE_REF="origin/${{ github.base_ref }}" HEAD_REF="HEAD" echo "Comparing API surface: $BASE_REF..$HEAD_REF" - node dist/cli.js "$BASE_REF..$HEAD_REF" --entrypoint src/index.ts || true + node dist/cli.js "$BASE_REF..$HEAD_REF" --entrypoint src/index.ts --check diff --git a/README.md b/README.md index c933e1c..4164367 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ npx sigdiff --entrypoint src/index.ts # JSON output npx sigdiff --json + +# Exit with code 1 if breaking changes are detected (useful for CI) +npx sigdiff --check ``` ## Example output @@ -67,6 +70,18 @@ Changes are classified as `major`, `minor`, or `patch` per semver rules. | Config required | None | Yes | Yes | Yes | | Git ref comparison | Any ref | N/A | N/A | Baseline file | +## CI integration + +Use `--check` to fail your pipeline when breaking changes are introduced: + +```yaml +# .github/workflows/ci.yml +- name: API surface check + run: npx sigdiff origin/main..HEAD --entrypoint src/index.ts --check +``` + +Without `--check`, sigdiff always exits 0 and just prints the diff. With `--check`, it exits 1 if any breaking changes are detected, making it easy to gate PRs. + ## Programmatic API ```typescript diff --git a/src/cli.ts b/src/cli.ts index d0fda3b..1c135f9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -14,7 +14,9 @@ cli .command('[range]', 'Diff the public API surface between two git refs') .option('--entrypoint ', 'Scope to a specific file') .option('--json', 'Output as JSON instead of markdown') - .action((range: string | undefined, options: { entrypoint?: string; json?: boolean }) => { + .option('--check', 'Exit with code 1 if breaking changes are detected') + .action( + (range: string | undefined, options: { entrypoint?: string; json?: boolean; check?: boolean }) => { try { assertGitRepo(); @@ -28,6 +30,10 @@ cli const output = format(result, { json: options.json }); process.stdout.write(output); + + if (options.check && result.breaking.length > 0) { + process.exit(1); + } } catch (err) { if (err instanceof SigdiffException) { process.stderr.write(`Error: ${err.error.message}\n`); @@ -35,7 +41,8 @@ cli } throw err; } - }); + }, +); cli.help(); cli.version(pkgVersion);