CI / Results on pull requests
Publish package previews of a pull request
A pull request fixes a bug in your library, and the person who reported the bug wants to try the fix before you release it. A package preview lets them install the fix with one line, with no clone, build or link of the branch.
6 min read
The packagePreview() step packs your packages in CI and puts them on the forge. A Package previews card at the top of the pull request lists them, with a copy button for the install command of each package.
You need:
- Write access to the repository on TanStack Source.
- A workflow that builds your packages. Your first workflow shows how to write one.
1. Add the preview job
Create .source/workflows/preview.ts:
import { defineWorkflow, packagePreview, pullRequest, setup } from '@tanstack-source/ci'
export default defineWorkflow('preview', {
on: [pullRequest({ branches: ['main'] })],
}).job('packages', {
permissions: { packages: 'preview' },
egress: ['registry.npmjs.org'],
steps: [
setup.pnpm(),
{ name: 'Install', run: 'pnpm install --frozen-lockfile' },
{ name: 'Build', run: 'pnpm build' },
packagePreview({ npm: ['packages/*'] }),
],
})
What each part does:
permissions: { packages: 'preview' }: the job can publish previews. Without it, the step uploads nothing and passes.Build: the step packs what is in each package folder, so it comes after the build.packagePreview({ npm: ['packages/*'] }): each folder that matchespackages/*and has apackage.jsonbecomes one tarball. Give a list of folders, globs, or both.
How the step packs:
- It uses pnpm when
packageManagerin your rootpackage.jsonnames pnpm. Else it uses npm. - A package that depends on another package of the same preview gets the preview URL of that package. The step changes
dependenciesandoptionalDependenciesfor the pack only, then puts yourpackage.jsonback. - Each
package.jsonneeds anameand aversion. Folders innode_modulesdo not count.
2. Push it to main
A pull request run takes the permissions of each job from the base branch. So push the workflow to main first:
git add .source/workflows/preview.ts
git commit -m "ci: add package previews"
git push origin main
Note
If a branch rule refuses the push, merge the workflow through a pull request. That pull request gets no preview, because its base branch does not have the job yet.
3. Open a pull request
- Push a branch, and open a pull request into
main. - Wait until the
previewrun ends. - Open the pull request. The Package previews card is at the top of the Conversation tab, under the description.
The card shows:
- The short commit of the preview, for example
4e5f6a7. - One row for each package, with its name, its version, a copy button and a download link.
- A switch for npm, pnpm, yarn and bun. It changes the command that each copy button copies, for example
yarn add <url>. - Copy all, which copies one command that installs every npm package of the list.
- With more than 6 rows, a filter field and a Show all button.
How the card changes:
- It shows the newest preview of the pull request. When a new preview job ends, the card shows its files, with no reload of the page.
- When you push again and the new preview is not ready yet, the card says that the preview is from an older commit.
- The files of older commits keep their URLs. They go with the other previews of the pull request: see When previews are deleted.
- In a private repository, the card also shows the
.npmrcline for a token. Assets that are public need no token.
The log of the step and the summary of the job show the same install lines.
Done
The people on your pull request now install the fix with one line. You released nothing.
Send the people who try the fix to Install a package preview.
Python packages
Give the files that your build writes. The node image has python3. This example needs the build package in the job:
{ name: 'Build', run: 'python3 -m build' },
packagePreview({ python: ['dist/*.whl', 'dist/*.tar.gz'] }),
The card gets a row for each file. Its copy button copies pip install <url>.
Other files
Give any other files in files, for example a CLI binary. The card gets a row for each file, with a download link and a copy button for its URL:
packagePreview({ files: ['dist/my-cli-linux-x64'] })
You can give npm, python and files in one step. Give at least one of them.
Runs with no pull request
A push run, a manual() run or a schedule() run can publish too. Its files go to package-previews/commits/<short commit>/, and no pull request shows them. The log and the job summary show the install lines.
In a local run (src ci run preview packages), the files go to ~/.cache/tanstack-source/package-previews/<short commit>/. The step prints that folder, and an install line with a file:// URL for each file.
Read the previews from an agent or a script
A coding agent or a script reads the same list from the packagePreviews field of the pull request:
src pr view 12 --json packagePreviews
{
"packagePreviews": {
"commit": "4e5f6a7",
"stale": false,
"isPrivate": false,
"files": [
{
"name": "@acme/core",
"version": "1.2.0",
"ecosystem": "npm",
"file": "acme-core-1.2.0.tgz",
"url": "https://forge.example/@acme/lib/assets/package-previews/12/4e5f6a7/acme-core-1.2.0.tgz"
}
]
}
}
- The field is
nullwhen the pull request has no preview. staleistruewhen the head moved after the preview.ecosystemisnpm,pythonorfiles.src pr view 12with no--jsonprintsPackage previews of 4e5f6a7, then one install line for each file.- REST (
GET /api/v1/repos/{owner}/{repo}/pulls/{number}) and the MCP toolget_pull_requestgive the same field.
Who can publish
The trust level of the run decides. Trust levels and secrets explains the levels.
| Run | Publishes |
|---|---|
protected | Yes |
trusted, for example a pull request from a person with write access | Yes |
untrusted, for example a pull request from a fork | No. The run shows the warning packages: 'preview' needs a trusted run; this run is untrusted. |
| A job of a private pull request | No. People outside the pull request can read the assets. |
| A job of a hidden workflow | No. It writes nothing that other people read. |
When the job cannot publish, the step uploads nothing, prints No package preview: with the reason, and passes.
When previews are deleted
The previews are assets of the repository, in the folder package-previews/ of the Assets tab. The forge deletes them:
- A pull request's folder (
package-previews/<number>/) goes 3 to 4 days after the pull request closes or merges. - If the pull request opens again before that, its previews stay.
- A folder of a run with no pull request (
package-previews/commits/<short commit>/) goes 3 to 4 days after its first upload.
The forge checks for due folders at least once a day, so a folder can stay up to one day longer than 3 days. After that, its URLs answer 404 Not Found.
Limits
- A file is at most 100 MiB by default. A site admin can change it: see Set the limits of the site.
- Previews count toward the 10 GiB of assets of the repository. See Assets.
- A file name has 1 to 200 characters, with no
/,\or control character. - One preview has at most 500 files.
The step fails when:
- A list matches no file. For
npm, nopackage.jsonmatches. - A
package.jsonhas nonameor noversion. - Two files have the same name. A preview folder keeps one file of each name.
- The forge refuses an upload, for example when the assets of the repository are at 10 GiB.