Open History to see the last commit
1# Stage public workspace packages to npm via OIDC trusted publishing.
2#
3# No NPM_TOKEN. The npm CLI (not bun) exchanges a GitHub OIDC token for a
4# short-lived, stage-only credential. A maintainer then 2FA-approves each
5# staged version on npmjs.com. See README "Releasing on npm".
6#
7# Trusted-publisher config on every package MUST match:
8# repository: modelschemas/modelschemas
9# workflow filename: publish.yml
10# environment: npm
11# allowed action: npm stage publish only
12#
13# On main, changesets keeps a Version Packages PR open while `.changeset/`
14# holds changesets. Merging it leaves none and a version npm lacks, so the
15# same run packs and stages that version.
16name: Publish
17
18on:
19 push:
20 branches: [main]
21 tags:
22 - 'v[0-9]*'
23 workflow_dispatch:
24 inputs:
25 dry_run:
26 description: Pack and verify tarballs; do not call npm stage publish
27 type: boolean
28 default: true
29
30concurrency:
31 group: npm-publish
32 cancel-in-progress: false
33
34permissions: {}
35
36jobs:
37 version:
38 if: github.ref == 'refs/heads/main' && github.event_name == 'push'
39 runs-on: ubuntu-latest
40 permissions:
41 contents: write
42 pull-requests: write
43 actions: write
44 outputs:
45 release: ${{ steps.check.outputs.release }}
46 steps:
47 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
48 with:
49 persist-credentials: false
50
51 - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
52 with:
53 no-cache: true # release jobs never restore a cache
54
55 - name: Install dependencies
56 run: bun install --frozen-lockfile
57
58 # Pinned: this step holds a contents/pull-requests write token.
59 # API pushes (the default) are signed; main requires signed commits.
60 - id: changesets
61 uses: changesets/action@ae32849d5ba541f9ae29e40e22a623bc13562f51 # v2.1.2
62 with:
63 version-script: bunx changeset version
64 pr-title: Version Packages
65 commit-message: Version Packages
66
67 - name: Run CI on the Version Packages PR
68 if: steps.changesets.outputs.pr-number != ''
69 env:
70 GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71 run: gh workflow run ci.yml --ref changeset-release/main
72
73 - id: check
74 if: steps.changesets.outputs.has-changesets == 'false'
75 run: |
76 v=$(jq -r .version packages/rate-card/package.json)
77 if npm view "@modelschemas/rate-card@$v" version >/dev/null 2>&1; then
78 echo "$v is already on npm"
79 else
80 echo "release=true" >> "$GITHUB_OUTPUT"
81 fi
82
83 ci:
84 needs: version
85 if: ${{ !cancelled() && (github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' || needs.version.outputs.release == 'true') }}
86 uses: ./.github/workflows/ci.yml # zizmor: ignore[self-repository] newer syntax; ./ is what GitHub documents for reusable workflows
87 permissions:
88 contents: read
89
90 pack:
91 needs: ci
92 if: ${{ !cancelled() && needs.ci.result == 'success' }}
93 runs-on: ubuntu-latest
94 permissions:
95 contents: read
96 actions: write
97 steps:
98 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
99 with:
100 persist-credentials: false
101
102 - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
103 with:
104 no-cache: true # release jobs never restore a cache
105
106 - name: Install dependencies
107 run: bun install --frozen-lockfile
108
109 # Builds packages that ship JS (rate-card: dist/ JS + .d.ts for edge
110 # runtimes) and refuses a tarball missing a file its manifest names.
111 - name: Build and pack public packages
112 env:
113 RELEASE_TAG: ${{ github.ref_type == 'tag' && github.ref_name || '' }}
114 run: bun scripts/npm-pack.ts --pack-dir dist/npm
115
116 - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
117 with:
118 name: npm-tarballs
119 path: dist/npm/*.tgz
120 if-no-files-found: error
121 retention-days: 7
122
123 stage:
124 needs: pack
125 if: ${{ !cancelled() && needs.pack.result == 'success' && (github.event_name == 'push' || !inputs.dry_run) }}
126 runs-on: ubuntu-latest
127 environment: npm
128 permissions:
129 contents: read
130 id-token: write
131 actions: read
132 steps:
133 - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
134 with:
135 name: npm-tarballs
136 path: tarballs
137
138 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
139 with:
140 node-version: '24'
141 registry-url: 'https://registry.npmjs.org'
142 package-manager-cache: false
143
144 - name: Install npm 11.19.0 (stage needs >= 11.15; npm 12 needs Node 24.15+)
145 run: npm install -g npm@11.19.0 # zizmor: ignore[adhoc-packages] exact version; stage publish needs npm, not bun
146
147 - name: Require npm >= 11.15.0
148 run: |
149 v=$(npm --version)
150 echo "npm $v"
151 maj=${v%%.*}
152 rest=${v#*.}
153 min=${rest%%.*}
154 if [ "$maj" -lt 11 ] || { [ "$maj" -eq 11 ] && [ "$min" -lt 15 ]; }; then
155 echo "npm >= 11.15.0 required for npm stage publish, got $v" >&2
156 exit 1
157 fi
158
159 - name: Stage tarballs
160 working-directory: tarballs
161 run: |
162 shopt -s nullglob
163 tarballs=(*.tgz)
164 if [ ${#tarballs[@]} -eq 0 ]; then
165 echo "no tarballs downloaded" >&2
166 exit 1
167 fi
168 for tgz in "${tarballs[@]}"; do
169 echo "staging $tgz"
170 npm stage publish "$tgz" --access public
171 done
172 echo "Staged ${#tarballs[@]} package(s). Approve with 2FA at"
173 echo "https://www.npmjs.com/ (Staged Packages), in order:"
174 echo " @modelschemas/client, @modelschemas/codegen,"
175 echo " @modelschemas/rate-card, @modelschemas/vite, modelschemas"