Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

publish.yml

179 lines6.1 KB

1# Stage public workspace packages to npm via OIDC trusted publishing.
2#
3# No NPM_TOKEN. The npm CLI 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
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: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
52 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
53 with:
54 node-version: 24
55 package-manager-cache: false
56​
57 - name: Install dependencies
58 run: pnpm install --frozen-lockfile
59​
60 # Pinned: this step holds a contents/pull-requests write token.
61 # API pushes (the default) are signed; main requires signed commits.
62 - id: changesets
63 uses: changesets/action@ae32849d5ba541f9ae29e40e22a623bc13562f51 # v2.1.2
64 with:
65 version-script: pnpm run version-packages # also refreshes pnpm-lock.yaml
66 pr-title: Version Packages
67 commit-message: Version Packages
68​
69 - name: Run CI on the Version Packages PR
70 if: steps.changesets.outputs.pr-number != ''
71 env:
72 GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73 run: gh workflow run ci.yml --ref changeset-release/main
74​
75 - id: check
76 if: steps.changesets.outputs.has-changesets == 'false'
77 run: |
78 v=$(jq -r .version packages/rate-card/package.json)
79 if npm view "@modelschemas/rate-card@$v" version >/dev/null 2>&1; then
80 echo "$v is already on npm"
81 else
82 echo "release=true" >> "$GITHUB_OUTPUT"
83 fi
84​
85 ci:
86 needs: version
87 if: ${{ !cancelled() && (github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' || needs.version.outputs.release == 'true') }}
88 uses: ./.github/workflows/ci.yml # zizmor: ignore[self-repository] newer syntax; ./ is what GitHub documents for reusable workflows
89 permissions:
90 contents: read
91​
92 pack:
93 needs: ci
94 if: ${{ !cancelled() && needs.ci.result == 'success' }}
95 runs-on: ubuntu-latest
96 permissions:
97 contents: read
98 actions: write
99 steps:
100 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
101 with:
102 persist-credentials: false
103​
104 - uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
105 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
106 with:
107 node-version: 24
108 package-manager-cache: false
109​
110 - name: Install dependencies
111 run: pnpm install --frozen-lockfile
112​
113 # Builds packages that ship JS (rate-card: dist/ JS + .d.ts for edge
114 # runtimes) and refuses a tarball missing a file its manifest names.
115 - name: Build and pack public packages
116 env:
117 RELEASE_TAG: ${{ github.ref_type == 'tag' && github.ref_name || '' }}
118 run: node --import ./scripts/register-hash-imports.mjs scripts/npm-pack.ts --pack-dir dist/npm
119​
120 - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
121 with:
122 name: npm-tarballs
123 path: dist/npm/*.tgz
124 if-no-files-found: error
125 retention-days: 7
126​
127 stage:
128 needs: pack
129 if: ${{ !cancelled() && needs.pack.result == 'success' && (github.event_name == 'push' || !inputs.dry_run) }}
130 runs-on: ubuntu-latest
131 environment: npm
132 permissions:
133 contents: read
134 id-token: write
135 actions: read
136 steps:
137 - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
138 with:
139 name: npm-tarballs
140 path: tarballs
141​
142 - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
143 with:
144 node-version: '24'
145 registry-url: 'https://registry.npmjs.org'
146 package-manager-cache: false
147​
148 - name: Install npm 11.19.0 (stage needs >= 11.15; npm 12 needs Node 24.15+)
149 run: npm install -g npm@11.19.0 # zizmor: ignore[adhoc-packages] exact version; stage publish needs this npm
150​
151 - name: Require npm >= 11.15.0
152 run: |
153 v=$(npm --version)
154 echo "npm $v"
155 maj=${v%%.*}
156 rest=${v#*.}
157 min=${rest%%.*}
158 if [ "$maj" -lt 11 ] || { [ "$maj" -eq 11 ] && [ "$min" -lt 15 ]; }; then
159 echo "npm >= 11.15.0 required for npm stage publish, got $v" >&2
160 exit 1
161 fi
162​
163 - name: Stage tarballs
164 working-directory: tarballs
165 run: |
166 shopt -s nullglob
167 tarballs=(*.tgz)
168 if [ ${#tarballs[@]} -eq 0 ]; then
169 echo "no tarballs downloaded" >&2
170 exit 1
171 fi
172 for tgz in "${tarballs[@]}"; do
173 echo "staging $tgz"
174 npm stage publish "$tgz" --access public
175 done
176 echo "Staged ${#tarballs[@]} package(s). Approve with 2FA at"
177 echo "https://www.npmjs.com/ (Staged Packages), in order:"
178 echo " @modelschemas/client, @modelschemas/codegen,"
179 echo " @modelschemas/rate-card, @modelschemas/vite, modelschemas"