Skip to content
Source
Sign inSign up

Show results on a pull request

CI / Results on pull requests

Show results on a pull request

Your workflow measures something on each pull request, like the bundle size or the test results, but reviewers must search the log for it. A surface puts the result on a live card at the top of the pull request.

4 min read

Each workflow gets one card on the Conversation tab, under the description. Its jobs write the card while they run, and reviewers see each change live.

You need:

  • Write access to the repository.
  • A pnpm repository whose pnpm build writes files to dist/. For other tools, change the commands in step 1.

1. Write a section

Create .source/workflows/bundle-size.ts:

import { readdir, stat } from 'node:fs/promises'
import { join } from 'node:path'
import { defineWorkflow, pullRequest, setup } from '@tanstack-source/ci'

export default defineWorkflow('bundle-size', { on: [pullRequest()] }).job('size', {
  egress: ['registry.npmjs.org'],
  steps: [
    setup.pnpm(),
    { name: 'Install', run: 'pnpm install --frozen-lockfile' },
    { name: 'Build', run: 'pnpm build' },
    {
      name: 'Report',
      do: async (ctx) => {
        const dist = join(ctx.workspace, 'dist')
        const files = (await readdir(dist, { withFileTypes: true })).filter((entry) => entry.isFile())
        const rows = await Promise.all(
          files.map(async (file) => {
            const { size } = await stat(join(dist, file.name))
            return `| ${file.name} | ${(size / 1024).toFixed(1)} KB |`
          }),
        )
        await ctx.surface.set('size', {
          title: 'Bundle size',
          markdown: ['| File | Size |', '|---|---|', ...rows].join('\n'),
        })
      },
    },
  ],
})

ctx.surface.set(key, section) writes one section of the card:

  • key: the name of the section, a lowercase slug like size. Letters a-z, digits and -, at most 40 characters.
  • title: optional. It shows above the Markdown.
  • markdown: the content. The page shows it with the same Markdown as a comment.

Run the job on your machine first. The step log shows the section, and nothing goes to the forge:

src ci run bundle-size size

2. Push it, then open a pull request

  1. Push the workflow to the default branch.
  2. Open a pull request.
  3. Open its Conversation tab.

Note

A pull request run takes the egress of its jobs from the base branch. So a job that is new in a pull request cannot reach the registry.

The bundle-size card is under the description. It shows:

  • The workflow name, the state of its run, the commit, and a View run link.
  • Each section: its title, then its Markdown.
  • From a fork, not trusted, when a run from a fork wrote it.
The bundle-size card on a pull request: a table of file sizes, a bar chart, two buttons and a report file to download

Click the workflow name to fold the card. Your browser remembers the fold for each card.

3. Update a section while the job runs

Call ctx.surface.set again with the same key. The new value replaces the section:

{
  name: 'Test',
  do: async (ctx) => {
    await ctx.surface.set('tests', { title: 'Tests', markdown: 'The tests run now.' })
    const { code } = await ctx.sh.nothrow`pnpm vitest run`
    const markdown = code === 0 ? 'All tests passed.' : `The tests failed with exit code ${code}.`
    await ctx.surface.set('tests', { title: 'Tests', markdown })
  },
},
  • Each key is its own section. A write changes only its own section, so two jobs that use different keys do not overwrite each other.
  • The card shows the sections in the order of their first write in the run.
  • A job sends at most one write a second for each key. When you write faster, the latest value wins, and the last value goes out before the job ends.

4. Add an image or a file

Upload a file that an earlier step made. Then use its name in the Markdown:

await ctx.surface.file('chart.png', 'out/chart.png')
await ctx.surface.set('chart', { title: 'Size over time', markdown: '![Size over time](chart.png)' })
FileHow the card shows it
PNG, JPEG, GIF and WebPAs images, where the Markdown uses them.
MP4 and WebMThey play at the end of the card.
Every other file, SVG and HTML tooAs a download link at the end of the card.
  • The path is relative to the workspace, or absolute.
  • A relative link or image in the Markdown points only to a file of the same surface.
  • A file name has letters, digits, ., _ and -, at most 100 characters, and does not start with ..

To delete the files when the pull request closes or merges, set the surface option. The Markdown stays:

export default defineWorkflow('bundle-size', {
  on: [pullRequest()],
  surface: { deleteFilesOnClose: true },
})

Done

Each pull request shows the bundle size at the top of its Conversation tab. The card changes while the job runs, and nobody opens a log to find the result.

To let a reviewer start more work from the card, add a button: Start a workflow from a button.

What happens on the next push

  • The card keeps the old content. It says From commit abc1234. The head is newer.
  • The first write of the new run clears all sections and files of the old run.
  • After that, a write from the old run fails.

Limits and rules

LimitValue
Sections20 for each surface
Markdown64 KB for each section
Buttons10 for each section
Files10 MB for each file, 50 MB for each surface
  • A workflow writes only its own surface.
  • A run with no pull request (push, schedule(), manual(), after()) writes no surface. Its step log shows one warning.
  • People who can read the pull request and its CI runs see the cards. The surface of a hidden workflow is for admins only.

To read the cards in a terminal, run src pr surface 7.

Installed apps show their own cards next to these, with a status on the Checks tab. See Cards and statuses.