diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 62228e0..9cd2c7c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,15 +1,32 @@ name: Release -# Only a `v*` tag publishes. workflow_dispatch exists so the whole pipeline can -# be rehearsed without releasing: it runs the verify job (lint, typecheck, -# tests, build, `pnpm pack`) and nothing else. Every step that publishes to npm -# or creates a GitHub release is gated on a real tag push, so a dispatch — on -# any branch, by anyone with write access — can never ship a version. A dispatch -# with action "move-next" instead runs only the move-next job, which re-points -# the npm `next` dist-tag at an already-published version and publishes nothing. +# Publishes @diffbot/ai-sdk to npm (trusted publishing, with provenance) and +# creates the matching GitHub Release. This file follows the shared npm release +# workflow standard (diffbot-dev-docs: docs/conventions/npm-release-workflow.md); +# keep it in sync with the other npm TypeScript repos. # -# npm auth is trusted publishing (OIDC). No NPM_TOKEN and no GitHub environment. -# The package must already exist on npm before a trusted publisher can be added. +# Jobs: verify (version / tag / CHANGELOG / private flag) -> gates -> publish +# (npm) -> github-release; move-next is a separate manual action. +# +# - Pushing a version tag (v1.2.3, v1.2.3-rc.0) is the only thing that publishes. +# - workflow_dispatch (action "dry-run", the default) runs every gate plus +# `npm publish --dry-run` and publishes nothing, even on a tag ref. +# - workflow_dispatch with action "move-next" only re-points the npm `next` +# dist-tag at an already-published version (see the move-next job). +# +# npm auth: trusted publishing (OIDC) only, bound to this file name. No token is +# passed to npm, so a misconfigured trusted publisher fails the publish instead of +# silently using a token. +# +# Versions: any version containing "-" (1.2.0-rc.0) publishes under the "next" +# dist-tag ("latest" is untouched) and its GitHub Release is a prerelease. A final +# version publishes as "latest" and then also moves "next". Release notes come +# from the "## [1.2.0-rc.0]" CHANGELOG section if there is one, otherwise from the +# base version's "## [1.2.0]" section. +# +# Repo-specific: package.json must not be "private": true; gates run once on +# Node 22 (lint, typecheck, unit tests, build, `pnpm pack`). The Node and `ai` peer +# matrices stay in ci.yml, and live tests stay in integration.yml. on: push: tags: ["v*"] @@ -31,19 +48,73 @@ on: required: false default: "" +permissions: + contents: read + +# Re-pushing a tag (or re-dispatching) queues behind an in-flight run for the same +# ref. A publish must never be cancelled halfway. concurrency: group: release-${{ github.ref }} cancel-in-progress: false -permissions: - contents: read - jobs: verify: - name: verify (lint, typecheck, test, build, pack) + name: Verify version # A move-next dispatch runs only the move-next job below. if: github.event_name != 'workflow_dispatch' || inputs.action != 'move-next' runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + version: ${{ steps.version.outputs.version }} + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + # Fail fast if the tag doesn't match package.json, so npm and the GitHub + # Release can never report different versions, or if the package is still + # marked private. + - name: Check tag matches package version + id: version + run: | + set -euo pipefail + pkg=$(node -p 'require("./package.json").version') + if [ "$(node -p 'String(require("./package.json").private === true)')" = "true" ]; then + echo '::error::package.json has "private": true; set it to false before releasing.' + exit 1 + fi + if [ "${GITHUB_REF_TYPE}" = "tag" ]; then + tag=${GITHUB_REF_NAME#v} + if [ "$pkg" != "$tag" ]; then + echo "::error::Tag v${tag} does not match package.json version ${pkg}. Bump package.json, then re-tag." + exit 1 + fi + fi + echo "version=${pkg}" >> "$GITHUB_OUTPUT" + echo "Versions agree: ${pkg}" + + # Checked before publishing so a tag without release notes fails before + # anything reaches npm. + - name: Check CHANGELOG.md has a section for this version + env: + VERSION: ${{ steps.version.outputs.version }} + run: | + set -euo pipefail + # A prerelease (1.2.0-rc.0) may reuse its base version's section (1.2.0). + if grep -qF "## [${VERSION}]" CHANGELOG.md; then + echo "Found '## [${VERSION}]'." + elif [ "${VERSION%%-*}" != "${VERSION}" ] && grep -qF "## [${VERSION%%-*}]" CHANGELOG.md; then + echo "Prerelease ${VERSION}: using the '## [${VERSION%%-*}]' section." + else + echo "::error::CHANGELOG.md has no '## [${VERSION}]' section." + exit 1 + fi + + gates: + name: Gates (lint, typecheck, test, build, pack) + needs: verify + runs-on: ubuntu-latest + timeout-minutes: 20 steps: - uses: actions/checkout@v4 with: @@ -58,52 +129,21 @@ jobs: - run: pnpm typecheck - run: pnpm test - run: pnpm build - # Dry run of the artifact itself: pack the tarball and list it, so the - # `files` field is proven to ship `dist/` + README + LICENSE and nothing - # else. Works while the package is still `"private": true` — and so does - # `pnpm publish --dry-run`, since npm's EPRIVATE check doesn't fire in - # dry-run mode; that would be the stronger rehearsal, exercising the real - # packing and registry-resolution path rather than just the tarball. + # Pack the tarball and list it, so the `files` field is proven to ship + # `dist/` + README + LICENSE and nothing else. - name: pnpm pack run: | pnpm pack --pack-destination "$RUNNER_TEMP" tar -tzf "$RUNNER_TEMP"/*.tgz - # Cheap pre-flight checks: a mistyped tag or an unflipped `private` flag is - # reported within seconds instead of at the end of a release. This job runs in - # parallel with `verify` rather than ahead of it, so it does not cut `verify` - # short — `publish` needs both. - guards: - name: tag and manifest guards - if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - persist-credentials: false - - name: Tag must match package.json version - run: | - version="$(node -p "require('./package.json').version")" - if [ "$GITHUB_REF_NAME" != "v$version" ]; then - echo "::error::Tag $GITHUB_REF_NAME does not match package.json version $version (expected v$version)." - exit 1 - fi - echo "Tag $GITHUB_REF_NAME matches package.json version $version." - - name: package.json must not be private - run: | - if [ "$(node -p "String(require('./package.json').private === true)")" = "true" ]; then - echo '::error::package.json still has "private": true — set it to false before tagging a release (PLAN.md §7.4 step 0c).' - exit 1 - fi - publish: - name: publish to npm - needs: [guards, verify] - if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + name: Publish to npm + needs: [verify, gates] runs-on: ubuntu-latest + timeout-minutes: 15 permissions: contents: read - id-token: write # npm provenance + id-token: write # npm trusted publishing (OIDC) and provenance steps: - uses: actions/checkout@v4 with: @@ -114,36 +154,50 @@ jobs: node-version: 22 cache: pnpm registry-url: https://registry.npmjs.org - - run: pnpm install --frozen-lockfile - - run: pnpm build - # Trusted publishing needs npm >= 11.5.1; Node 22 ships older. Use npm, - # not pnpm, so the OIDC token exchange works. No token is set. + # Trusted publishing needs npm >= 11.5.1 and `npm dist-tag` over OIDC needs + # >= 11.21.0; Node 22 bundles npm 10. Pinned to a minimum, not `latest`. - name: Update npm run: | - npm install -g npm@latest + npm install -g npm@^11.21.0 npm --version + - run: pnpm install --frozen-lockfile + - run: pnpm build - # The dist-tag is derived from the version, not from the tag name: a - # prerelease (anything containing a `-`, e.g. 0.1.0-rc.1) publishes as - # `next` so a plain `npm install @diffbot/ai-sdk` keeps resolving to the - # last stable release. The guards above already proved the git tag equals - # this version. - - name: Publish to npm + # npm, not pnpm, so the OIDC token exchange works. --ignore-scripts: dist/ was + # built above, so no lifecycle script may rebuild it. + - name: Publish + env: + VERSION: ${{ needs.verify.outputs.version }} run: | set -euo pipefail - name="$(node -p "require('./package.json').name")" - version="$(node -p "require('./package.json').version")" - case "$version" in - *-*) dist_tag=next ;; - *) dist_tag=latest ;; - esac - if npm view "${name}@${version}" version >/dev/null 2>&1; then - echo "${name}@${version} is already on npm; skipping publish." + name=$(node -p 'require("./package.json").name') + # The dist-tag comes from the version, not the tag name: a prerelease + # (1.2.0-rc.0) goes to "next", never "latest". + case "$VERSION" in *-*) dist_tag=next ;; *) dist_tag=latest ;; esac + + # Only a tag push publishes. A manual dispatch (even on a tag ref) is a dry run. + # npm >= 11.21 errors on `npm publish --dry-run` if the version is already + # on the registry, so already-published versions are packed only. + if [ "${GITHUB_EVENT_NAME}" != "push" ] || [ "${GITHUB_REF_TYPE}" != "tag" ]; then + echo "Not a tag push: dry run only." + if npm view "${name}@${VERSION}" version >/dev/null 2>&1; then + echo "${name}@${VERSION} is already on npm; packing only." + npm pack --pack-destination "${RUNNER_TEMP}" + tar -tzf "${RUNNER_TEMP}"/*.tgz + else + npm publish --dry-run --access public --ignore-scripts --tag "$dist_tag" + fi exit 0 fi - echo "Publishing $version with --tag $dist_tag (npm trusted publishing)." - # --ignore-scripts: dist/ was built above. No NODE_AUTH_TOKEN; npm - # authenticates with the trusted publisher on npmjs.com. + + # Lets a failed run be re-run after the npm step already succeeded. + if npm view "${name}@${VERSION}" version >/dev/null 2>&1; then + echo "${name}@${VERSION} is already on npm; skipping publish." + exit 0 + fi + + # Trusted publishing (OIDC) only; no token is exported. + echo "Publishing ${name}@${VERSION} with dist-tag ${dist_tag} via npm trusted publishing." npm publish --access public --provenance --ignore-scripts --tag "$dist_tag" # A final release also moves `next`, so `next` is never behind `latest`. @@ -167,29 +221,62 @@ jobs: fi github-release: - name: create GitHub release - needs: publish - if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + name: Publish GitHub Release + needs: [verify, publish] runs-on: ubuntu-latest + timeout-minutes: 5 + # Only for real tag pushes; a dispatch runs just the gates + publish dry run. + if: github.event_name == 'push' && github.ref_type == 'tag' permissions: - contents: write # the only job that writes to the repo + contents: write steps: - uses: actions/checkout@v4 with: persist-credentials: false - - name: gh release create + + - name: Extract release notes from CHANGELOG.md + env: + VERSION: ${{ needs.verify.outputs.version }} + run: | + set -euo pipefail + # Everything after "## [VERSION]" up to the next "## " heading. A prerelease + # without its own section uses its base version's section. + hdr_version="${VERSION}" + if ! grep -qF "## [${VERSION}]" CHANGELOG.md; then + hdr_version="${VERSION%%-*}" + fi + awk -v hdr="## [${hdr_version}]" ' + index($0, hdr) == 1 { found = 1; next } + found && /^## / { exit } + found { print } + ' CHANGELOG.md > "${RUNNER_TEMP}/notes.md" + if ! grep -q '[^[:space:]]' "${RUNNER_TEMP}/notes.md"; then + echo "::error::CHANGELOG.md section for ${VERSION} is empty." + exit 1 + fi + wc -l "${RUNNER_TEMP}/notes.md" + + - name: Publish release env: GH_TOKEN: ${{ github.token }} + VERSION: ${{ needs.verify.outputs.version }} run: | set -euo pipefail - if gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then - echo "Release $GITHUB_REF_NAME already exists; leaving it." - else - gh release create "$GITHUB_REF_NAME" \ - --repo "$GITHUB_REPOSITORY" \ - --title "$GITHUB_REF_NAME" \ - --generate-notes + tag="${GITHUB_REF_NAME}" + repo="${GITHUB_REPOSITORY}" + if gh release view "$tag" --repo "$repo" >/dev/null 2>&1; then + echo "Release $tag already exists; leaving its notes as they are." + exit 0 fi + # Any version containing "-" (1.2.0-rc.0) is a GitHub prerelease. + prerelease=() + case "$VERSION" in *-*) prerelease=(--prerelease) ;; esac + gh release create "$tag" \ + --repo "$repo" \ + --title "$tag" \ + --verify-tag \ + --notes-file "${RUNNER_TEMP}/notes.md" \ + "${prerelease[@]}" # Manual only (action: move-next). Points `next` at a version that is already # on npm, e.g. after a final release whose "Point next at final release" step @@ -216,7 +303,7 @@ jobs: registry-url: https://registry.npmjs.org - name: Update npm run: | - npm install -g npm@latest + npm install -g npm@^11.21.0 npm --version - name: Point next at an existing version env: diff --git a/CHANGELOG.md b/CHANGELOG.md index 74329e5..6fa7108 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 0.1.0 - 2026-11-17 +## [0.1.0] - 2026-11-17 First API of `@diffbot/ai-sdk`: Diffbot tools, retrieval helpers, and an OpenAI-compatible provider for the Vercel AI SDK (`ai` ^6 or ^7). diff --git a/CLAUDE.md b/CLAUDE.md index 652c661..aa6c4f8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -22,7 +22,7 @@ Four workflows: - `ci.yml` — unit-only, **no secrets** (lint, typecheck, unit tests + build on Node 20/22/24, the `ai@6`/`ai@7` peer matrix, empty-token live-skip guard, Workers bundle check). Keep it secret-free so it can run on fork PRs. - `integration.yml` — `pnpm test:live`, on push to `main`, a twice-monthly drift schedule (1st and 15th at 14:17 UTC), and `workflow_dispatch`. Never `pull_request`/`pull_request_target`, so `DIFFBOT_API_TOKEN` is never exposed to fork PR code. A guard step fails the run on a missing or blank secret rather than skipping everything green. - `canary.yml` — weekly `ai@latest` + `@ai-sdk/openai-compatible@latest` typecheck + unit tests. No secrets. -- `release.yml` — `v*` tag only; `workflow_dispatch` is a build-only rehearsal and every publish/release step is gated on the tag push. It fails early unless the tag equals `package.json`'s version and `"private"` is not `true`. Publish uses npm trusted publishing (OIDC). There is no `NPM_TOKEN`. `@diffbot/ai-sdk` is not on npm yet, so a trusted publisher cannot be added until a one-time manual bootstrap publish creates the package. +- `release.yml` — `v*` tag only; follows the shared npm release workflow standard (diffbot-dev-docs `docs/conventions/npm-release-workflow.md`). Jobs: `verify` (tag equals `package.json`'s version, `"private"` is not `true`, `CHANGELOG.md` has a `## [X.Y.Z]` section) → `gates` (lint, typecheck, test, build, `pnpm pack` on Node 22) → `publish` (npm trusted publishing over OIDC, no `NPM_TOKEN`; `-` versions go to `next`, finals to `latest` and then also move `next`) → `github-release` (notes from the CHANGELOG section, `--prerelease` for `-` versions). `workflow_dispatch` is a dry run (gates + `npm publish --dry-run`, or `npm pack` if the version is already on npm) and cannot ship; action `move-next` only retags an already-published version. `pnpm test:workers` dry-run bundles `tests/fixtures/worker/` for workerd and then scans the bundle: `wrangler deploy --dry-run` alone leaves static `node:*` imports in the output as externals, so the scan is what actually enforces Workers-cleanliness. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d4aefbb..968e0af 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -36,11 +36,23 @@ restore `pnpm-lock.yaml` before committing. | `ci.yml` | push to `main`, every `pull_request`, dispatch | **none** | | `integration.yml` | push to `main`, 1st + 15th at 14:17 UTC, dispatch | `DIFFBOT_API_TOKEN` | | `canary.yml` | Mondays at 14:17 UTC, dispatch | **none** | -| `release.yml` | `v*` tag; dispatch is a build-only rehearsal | **none** (npm trusted publishing) | +| `release.yml` | `v*` tag; dispatch is a dry-run rehearsal | **none** (npm trusted publishing) | `ci.yml` runs lint + typecheck, unit tests + build on Node 20 / 22 / 24, the peer matrix (`ai@6` + `@ai-sdk/openai-compatible@2`, `ai@7` + `@3`), the empty-token live-skip guard, and the Workers bundle check. **It must stay secret-free** — that is what lets every job run on fork PRs. -`release.yml` publishes only from a `v*` tag push: it checks the tag equals `package.json`'s `version` and that `"private"` is not `true`, runs lint/typecheck/tests/build, then `npm publish --access public --provenance` via trusted publishing (OIDC, no `NPM_TOKEN`), then creates the GitHub release. `workflow_dispatch` stops after `pnpm pack`; every publishing step is gated on the tag push, so a dispatch cannot ship a version. To release: bump `version`, commit, tag `vX.Y.Z`, push the tag. npm cannot attach a trusted publisher until the package exists; the first publish of `@diffbot/ai-sdk` needs a one-time manual bootstrap (see the release report) before that tag will succeed. +`release.yml` follows the shared npm release workflow standard for the four Diffbot TypeScript packages (`docs/conventions/npm-release-workflow.md` in diffbot-dev-docs). Its jobs: + +1. `verify` — the tag equals `package.json`'s `version`, `"private"` is not `true`, and `CHANGELOG.md` has a `## [X.Y.Z]` section for the version (a prerelease such as `0.2.0-rc.0` may reuse `## [0.2.0]`). +2. `gates` — lint, typecheck, unit tests, build and `pnpm pack` on Node 22. The Node and peer matrices stay in `ci.yml`; live tests stay in `integration.yml`. +3. `publish` — `npm publish --access public --provenance` via trusted publishing (OIDC, no `NPM_TOKEN`). The dist-tag comes from the version: anything containing `-` goes to `next`, otherwise `latest`, and a final release then also moves `next`. A version already on npm is skipped, so a failed run can be re-run. +4. `github-release` — creates the GitHub Release with notes taken from that CHANGELOG section, marked as a prerelease when the version contains `-`. + +Only a tag push publishes. A `workflow_dispatch` (action `dry-run`, the default) runs `verify`, `gates` and a publish rehearsal (`npm publish --dry-run`, or `npm pack` if the version is already on npm), and publishes nothing, even on a tag ref. Action `move-next` only re-points the npm `next` dist-tag at an already-published version. + +To release: + +- **Release candidate:** set `version` to `X.Y.Z-rc.N`, make sure `CHANGELOG.md` has a `## [X.Y.Z-rc.N]` or `## [X.Y.Z]` section, merge, then tag `vX.Y.Z-rc.N` and push the tag. npm `next` moves; `latest` does not. +- **Final:** set `version` to `X.Y.Z` with a `## [X.Y.Z] - YYYY-MM-DD` section, merge, then tag `vX.Y.Z` and push the tag. npm `latest` and `next` both move. ## Live tests diff --git a/README.md b/README.md index 641d31e..08a9c76 100644 --- a/README.md +++ b/README.md @@ -717,8 +717,9 @@ and needs no Cloudflare account. See [`CONTRIBUTING.md`](./CONTRIBUTING.md) for ## Releasing [`CONTRIBUTING.md`](./CONTRIBUTING.md) is the release process. `release.yml` publishes from a `v*` -tag that matches `package.json`'s `version`. A manual dispatch of that workflow packs a tarball and -stops there. +tag that matches `package.json`'s `version` and has a `## [X.Y.Z]` section in `CHANGELOG.md`; the +GitHub Release notes come from that section, and a version containing `-` is published to npm +`next` and marked as a GitHub prerelease. A manual dispatch of that workflow runs the gates and a publish rehearsal and publishes nothing. ## License