Skip to content

Triggers

Run an extension tool once for each commit or merge that advances a branch.

Updated

A trigger runs one of an extension’s tools once for each advance of a branch that it follows. An advance is anything that moves the branch’s head: a commit on the branch, a merge commit, or a fast-forward merge. Use a trigger to react to changes as they land, such as auditing what a commit changed or syncing changed records to another service.

Like a schedule, a trigger isn’t a separate kind of code. Each run is an ordinary call of one of the extension’s tools, which takes the advance as its input.

Trigger declarations in index.ts

Import onBranchAdvance and branchAdvance from @buildr/sdk and pass your triggers under triggers in defineExtension:

import { branchAdvance, defineExtension, onBranchAdvance, tool } from "@buildr/sdk";

const audit = tool(
  {
    description: "Records what an advance of main changed",
    input: branchAdvance,
  },
  async (ctx, advance) => {
    let changes = 0;
    let cursor: string | null = null;
    do {
      const page = (await ctx.forge.diffs.getRepoDiff({
        repo_id: ctx.repo.id,
        base_commit_id: advance.fromCommitId,
        head_commit_id: advance.toCommitId,
        ...(cursor === null ? {} : { cursor }),
      })) as { data: ReadonlyArray<unknown>; next_cursor: string | null };
      changes += page.data.length;
      cursor = page.next_cursor;
    } while (cursor !== null);
    return { kind: advance.kind, changes };
  },
);

export default defineExtension(
  {},
  {
    tools: { audit },
    triggers: {
      auditMain: onBranchAdvance({ branches: ["main"] }, audit),
    },
  },
);

The tool receives the advance as a branchAdvance value with these fields:

  • branch: The name of the branch that advanced.
  • fromCommitId: The branch’s head commit before the advance.
  • toCommitId: The branch’s head commit after the advance.
  • kind: "commit" for a commit on the branch, "merge" for a merge commit, or "fast_forward" for a fast-forward merge, which can bring several commits at once.

To read what the advance changed, diff its two commits with ctx.forge.diffs.getRepoDiff, as in the example. A diff comes a page at a time; pass each page’s next_cursor back as cursor until it is null. A workflow step runs under a timeout, so a workflow that reads a large diff can read a page per step.

Trigger validation rules

Publishing an extension checks every entry in triggers, and a trigger that breaks a rule fails the publication with DeclarationFailed:

  • Trigger name: Each key in triggers must be a valid JavaScript identifier (/^[A-Za-z_][A-Za-z0-9_]*$/) of at most 128 characters.
  • Branches: branches must name at least one branch, and no name can be empty.
  • Target tool: The tool passed to onBranchAdvance must also be in the extension’s tools tree.
  • Input: The tool’s input must accept an advance of each branch the trigger follows. Use branchAdvance itself, or a value type that accepts the same fields.

Where triggers run

A repo’s main decides which triggers run in that repo. A trigger has no on or off setting: it runs once the version that main runs declares it.

Repo Version whose triggers run
A repo that installed the extension The release that main’s head commit pins
The repo that defines the extension The version that main’s head commit publishes, prereleases included

A trigger follows the branches it names in that repo, which can include branches other than main. Its tool’s ctx.branch is the run’s own branch, which merges into main. That merge doesn’t run the same trigger again, though it runs the extension’s other triggers and other extensions’ triggers that follow main. Read branch from the advance to know which branch moved.

These rules decide which advances a trigger runs for:

  • From the install on: A trigger runs for advances after the commit that installed the extension or first declared the trigger, never for the commit that did. In the repo that defines the extension, the trigger starts once the version that declares it has published on main.
  • In order: Forge queues each advance when the branch moves and runs the queue in the order that the advances happened, one run for each advance.
  • At the version main runs then: Each run uses the version that main runs when the run starts, which can be newer than the version main ran when the branch moved.
  • Waiting and dropped advances: In the repo that defines the extension, queued advances wait while the version that main’s head publishes is still publishing. Queued advances are dropped when that version drops the trigger or fails to publish, or when the extension is deleted or uninstalled.

Forge doesn’t keep what a trigger’s tool returns, and it never retries a run that failed or was interrupted. To run one again, see Manual runs and run history. A tool that commits to a branch its own trigger follows makes another advance, so the trigger runs again. Commit to a different branch, as an audit log might, or make sure that the tool stops on its own.

Manual runs and run history

Forge records each run of a trigger with the advance it ran for, its status, and its start and finish times. A run’s status is one of these values:

  • running: The tool call hasn’t ended.
  • succeeded: The tool returned.
  • failed: The tool threw, and the run’s failure holds one line of the error.
  • interrupted: Nothing reported how the run ended within 2 minutes of its start.

Each trigger keeps its latest 100 runs. A member with readRuns on the trigger (Actions) lists them, newest first, for main’s installation or for an extension that the repo defines:

forge extensions list-extension-installation-trigger-runs --repo "$OPS_REPO" \
  --name integrations.audit --trigger auditMain

forge extensions list-extension-trigger-runs --repo "$DIRECTORY_REPO" \
  --name audit --trigger auditMain

A member with run on the trigger can run it again for the advance that its latest run handled. The run acts as the extension, as the trigger’s own runs do, and answers once its tool call ends:

forge extensions create-extension-installation-trigger-run --repo "$OPS_REPO" \
  --name integrations.audit --trigger auditMain

forge extensions create-extension-trigger-run --repo "$DIRECTORY_REPO" \
  --name audit --trigger auditMain

A trigger runs once at a time, so a manual run fails with ExtensionTriggerRunActive (HTTP 409) while another run of the trigger is in flight. A trigger that has never run has no advance to run again, and fails with ExtensionTriggerNeverRan (HTTP 409).

To find triggers and schedules whose latest finished run failed or was interrupted, in every repo that you can read in an organization, run forge extensions get-automation-health --org "$ORG" (GET /orgs/:org_id/automation-health). It lists only the triggers and schedules that you have readRuns on, which the default Editor policy doesn’t grant, so an empty answer doesn’t always mean that nothing failed.

Execution identity and connections

A trigger run has no interactive caller. Its ctx.forge calls act as the extension itself, under the policies that name it. Like a scheduled run, a trigger run holds only the repo’s bound org connections, and reading ctx.user.connections.<name> throws.

A trigger’s tool can start a workflow with ctx.workflows.start. The run reads main and acts as the extension too.

Type to search…

↑↓ navigate↵ selectEsc close