Repository navigation
feat(publish-preview-oidc): publish tarballs packed in the build job - #348
Conversation
The publish job used to overlay workspace files from the pull request build on top of a checkout of the default branch, then run `yarn install` to reconcile the two. That mixed trusted and untrusted files in the job that holds `id-token: write`. Now the build job packs the packages and uploads only the tarballs. The publish job no longer checks out the repository or runs Yarn. It validates each tarball (single `package/` directory, exactly one manifest, name in the preview scope, preview version, restricted `publishConfig`) and publishes it with `npm publish --ignore-scripts`. `prepare-preview.yml` gains a `pack` input, off by default, so the token-based `publish-preview.yml` workflow keeps working as before.
The tarball validation compared `publishConfig.registry` against `https://registry.npmjs.org/` exactly, so a manifest using `https://registry.npmjs.org` failed the job even though both point to the same registry. Accept both forms and nothing else, so lookalike hosts are still rejected.
| run: echo "Dry run — skipping publish" | ||
|
|
||
| - name: Generate preview build message | ||
| - name: Generate preview build message (monorepo) |
There was a problem hiding this comment.
Split this into a separate step for monorepos and polyrepos, so it's a bit more readable.
| if: ${{ inputs.pull-request }} | ||
| run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt | ||
| env: | ||
| GH_REPO: ${{ github.repository }} |
There was a problem hiding this comment.
Without a checkout, the gh CLI needs this specified explicitly.
There was a problem hiding this comment.
Recommended to review this without whitespace.
|
|
||
| for location in "${locations[@]}"; do | ||
| manifest="$location/package.json" | ||
| jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$manifest" > temp.json |
There was a problem hiding this comment.
We previously did this in the publish workflow, but since we're now packing here, we need to do this here.
| environment: ${{ inputs.environment }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout and setup environment |
There was a problem hiding this comment.
AI pointed this:
This job doesn't set up Node any more, so npm publish uses the runner's npm. OIDC publishing needs npm 11.5.1 or newer.
With an older npm, the publish fails at the token exchange with an auth error. Can we add actions/setup-node with node-version: 24 before the publish steps? so we don't rely on the runner's node version
There was a problem hiding this comment.
I initially added actions/setup-node here for that reason, but removed it later because I noticed GitHub includes NPM 11.19.1 by default in the latest image.
If we add something like node-version: '24', it's another place where we need to maintain a Node.js version. We already have some actions in this repository still using Node.js 18 etc. 😅 And we can't reference an .nvmrc without doing a checkout, which I tried to avoid.
There was a problem hiding this comment.
I guess we can rely on github keeping everything in sync then 👍
The steps that select and pack packages filled arrays with `mapfile` from a process substitution. GitHub runs steps with `bash -e` but without `pipefail`, so a failing `yarn workspaces list` or `jq` left the array empty and the step passed. Capture the output in a variable first, so a failure stops the step. Call `mapfile` only when there is output, because it turns empty input into an array with one empty element.
The publish job in
publish-preview-oidcchecks out the default branch, overlays the workspace from the pull request build (package.jsonfiles,yarn.lock, andpackages/*/), and runsyarn installto reconcile the two. As a result, the job that holdsid-token: writemixes trusted files from the default branch with untrusted files from the pull request, and it needs a sanitisation step to strip lifecycle scripts and registry overrides before publishing.With this change, the build job packs the packages and uploads only the tarballs. The publish job no longer checks out the repository or runs Yarn.
prepare-previewhas a newpackinput, which is off by default. When it is enabled, the workflow strips pack and publish lifecycle scripts from the selected packages, runsyarn packfor each one, and uploads the tarballs aspreview-build-tarballs. The token-basedpublish-previewworkflow doesn't set the input, so its behaviour is unchanged.package/directory and exactly onepackage/package.json. The manifest must have a name in the preview scope, a-preview-<sha>version, and apublishConfigthat sets onlyaccessand the NPM registry. Because NPM strips the top-level directory before it reads the manifest, a crafted tarball could otherwise make NPM use a different manifest from the one that was validated.npm publish <file>, using the NPM bundled with the runner.Note
Medium Risk
Changes the trusted path for npm publishing and OIDC credentials; validation is stricter but any gap in tarball checks could still be security-sensitive.
Overview
Preview publishing for
publish-preview-oidcno longer ships the whole workspace into the job that hasid-token: write. The reusableprepare-previewworkflow gains apackinput (default false); when enabled it strips pack/publish lifecycle scripts, runsyarn packfor the selected packages, and uploadspreview-build-tarballsinstead ofpreview-build-artifacts.The OIDC publish job now downloads those tarballs only—no checkout, Yarn, or manifest sanitization on a mixed trusted/untrusted tree. It validates each
.tgz(layout underpackage/, preview scope name,-preview-<sha>version, allowedpublishConfig) then publishes withnpm publish(--ignore-scripts, fixed registry). Monorepo/polyrepo PR comment generation is split so polyrepo reads the version from the tarball.Shell fixes in package selection avoid empty
mapfileissues whenjqreturns nothing.Reviewed by Cursor Bugbot for commit 9e2dc1e. Bugbot is set up for automated code reviews on this repo. Configure here.