Skip to content
LogoLogo

How to run code when a workspace publishes

This guide shows you how to run code on your server each time a workspace publishes in a project that has your app. For example, you can start a build of a site, or clear a cache.

You need an app with a server that uses @picked/app-kit, and a public endpoint. Build and install your first app makes these.

1. Subscribe to the event

Add workspace.published to events in the manifest. The manifest must have an endpoint, because Picked sends each event to <endpoint>/events.

picked.config.ts
import { defineConfig } from '@picked/config/define'
 
export default defineConfig({
  app: {
    handle: 'my-app',
    name: 'My App',
    endpoint: 'https://my-app.example.com',
    events: ['workspace.published'],
  },
})

workspace.published is the only event.

2. Handle the event

Give createAppHandler an onEvent function. createAppHandler checks that Picked signed the request, then calls onEvent with the event:

src/server.ts
import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
import { serve } from '@picked/app-kit/node'
 
const port = process.env.APP_PORT ?? '4103'
const { pickedKeys } = readAppEnvironment(process.env, port)
 
const handler = createAppHandler({
  pickedKeys,
  async onEvent(event) {
    if (event.type !== 'workspace.published') return
    console.log(
      `Project ${event.projectId} published version ${event.workspaceVersionId}`,
      `of workspace ${event.workspaceId} at ${event.occurredAt}`,
    )
  },
})
 
serve(handler, Number(port))

Use these fields of the event:

  • workspaceId. The workspace that was published.
  • workspaceVersionId. The version that Picked wrote when it published the workspace.
  • eventId. The ID of the event. Step 3 shows how to use it.
  • token. A token for the Picked API, with the scopes of the installation. It expires after one hour.

App events gives each field.

createAppHandler answers 200 after onEvent resolves. Picked waits 15 seconds at most for the answer, resends included. Keep the work in onEvent short.

The workspace publishes also when your server fails. Picked does not show your answer to the person who publishes.

3. Skip an event that you already handled

Picked can send one event more than one time. It sends the event again after a 408, a 429 or a 5xx answer, or when it gets no answer. Picked can also send the same event again at a later time. Each send has the same eventId.

Keep the eventId of the last event that you handled. If an event has that eventId, do nothing. The Vercel app of Picked keeps it in a label of the installation:

src/server.ts
import { createAppHandler, installationClientFor, readAppEnvironment } from '@picked/app-kit/server'
import { serve } from '@picked/app-kit/node'
 
const port = process.env.APP_PORT ?? '4103'
const { pickedKeys, privateKey } = readAppEnvironment(process.env, port)
 
const LAST_EVENT = { namespace: 'my-app', key: 'last-event' }
 
const handler = createAppHandler({
  pickedKeys,
  async onEvent(event) {
    const client = installationClientFor(event, { privateKey })
    const last = await client.labels.get(LAST_EVENT.namespace, LAST_EVENT.key)
    if (last?.value === event.eventId) return
 
    await client.labels.set([{ ...LAST_EVENT, type: 'text', value: event.eventId }])
    await startBuild(event.projectId, event.workspaceVersionId)
  },
})
 
async function startBuild(projectId: string, workspaceVersionId: string): Promise<void> {
  await fetch('https://builds.example.com/api/builds', {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ projectId, workspaceVersionId }),
  })
}
 
serve(handler, Number(port))

installationClientFor uses the token of the event. That token can read and write the labels of the installation without a scope. To read other project data, add the scopes that you need to scopes in the manifest. How to read project data from an app shows how.

4. Deploy and upgrade

  1. Deploy a new version of the manifest.
  2. Upgrade each installation to that version.
  3. Publish a workspace in a project that has the app.

Picked sends an event only to an installation whose version lists the event in events. An installation that uses an earlier version gets no events until it is upgraded.

Add a button to the publish modal

To let a person start the work by hand, add a page to the publish modal. Declare a slot with the location workspace-postpublish-actions. The page shows in the modal after Picked publishes the workspace. The Vercel app of Picked shows a Deploy <project> button there for each Vercel project that it deploys.

How to add a page to the dashboard shows how.