Skip to content
Source
Sign inSign up

Start a workflow from a button

CI / Results on pull requests

Start a workflow from a button

A reviewer wants more work on a pull request: a blog post draft, a preview or a slow benchmark. That work is too slow or too costly for each push, so a button on the card of a workflow starts it in one click.

4 min read

A button on the surface of a workflow starts another workflow on the pull request. The reviewer clicks once, and the new run writes its own card.

You need:

1. Let the workflow start from a button

Create .source/workflows/blog-post.ts:

import { button, defineWorkflow } from '@tanstack-source/ci'

export default defineWorkflow('blog-post', {
  on: [button({ inputs: { tone: { type: 'choice', options: ['casual', 'formal'], default: 'casual' } } })],
}).job('draft', {
  steps: [
    {
      name: 'Draft',
      do: async (ctx) => {
        await ctx.surface.set('post', {
          title: 'Blog post draft',
          markdown: `A ${ctx.inputs.tone} post about this change goes here.`,
        })
      },
    },
  ],
})
  • Without a button() trigger, no button can start the workflow.
  • inputs has the rules of manual inputs. The steps get the values in ctx.inputs.
  • A workflow has at most one button(). When it has manual() too, declare the inputs in only one of the two. Both use them.
  • The run writes the surface of blog-post, not the surface with the button.

2. Add the button

In the Report step of bundle-size, add buttons to the section:

await ctx.surface.set('size', {
  title: 'Bundle size',
  markdown: ['| File | Size |', '|---|---|', ...rows].join('\n'),
  buttons: [
    { label: 'Write blog post', workflow: 'blog-post', inputs: { tone: 'casual' } },
    { label: 'Open the docs', url: 'https://tanstack.com/' },
  ],
})

A section has two kinds of button:

  • Workflow button: starts the workflow on the pull request, with these fixed input values.
  • URL button: opens an https URL in a new tab. The card shows the host next to the label, like Open the docs (tanstack.com).

The id of a button is the slug of its label, like write-blog-post. To pick another, give id. Each id is unique in its section.

3. Push both workflows

  1. Push blog-post.ts and the change to bundle-size.ts to the default branch.
  2. Open a pull request from a branch that has both files.

A click takes the steps of the workflow from the head of the pull request. It takes the secrets, permissions and egress from the base branch, as every pull request run does.

4. Click the button

On the Conversation tab, the bundle-size card shows Write blog post. Click it.

In a terminal, give the pull request, the workflow of the card and the id of the button:

src pr surface click 7 bundle-size write-blog-post
The bundle-size card with the Write blog post workflow button and the Open preview URL button

Under the button, the card now shows the state of the run and a link to it. Other people also see who started it.

Who can click:

  • People with write access, on an open pull request. The button is off for other people, with the reason.
  • A personal token works in src, REST and MCP. The forge refuses an agent's token and a job token.

Done

A reviewer clicks Write blog post on the bundle size card. A blog-post card shows up on the same pull request, with the draft, and the button links to its run.

What a click starts

  • A pull request run of blog-post, on the head commit of the pull request.
  • The values of the button go to the inputs. A missing value takes its default. An unknown input, a value of the wrong type, or a missing required value refuses the click.
  • The run gets the trust level of the pull request. A button on a pull request from a fork starts an untrusted run, so a click gives the fork no more access.
  • A workflow that is turned off refuses the click.
  • The run is not a check. It does not show in the checks of the pull request, and it never holds back a merge or merge when ready. Its card and the CI tab link to it.

To click from a script, use POST /api/v1/repos/<owner>/<repo>/pulls/<n>/surfaces/<workflow>/buttons/<button>/click or the MCP tool click_surface_button.