Sub topic 17
Where to go next: workflow_call, workflow_run and repository_dispatch
This chapter assumes you know the on key, the four main events (push, pullrequest, schedule, workflowdispatch) and how to read github.event_name. Three more events connect workflows to each other or to outside systems. Each deserves its own study, so this chapter only shows when to reach for each one.
Choosing a trigger
workflow_call: reusable workflows
workflow_call turns a workflow into a reusable one that other workflows call as a job. It declares typed inputs and, optionally, secrets and outputs.
# .github/workflows/build.yml
on:
workflow_call:
inputs:
node-version:
type: string
required: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Building with Node ${{ inputs.node-version }}"The caller references it with uses: at the job level:
# .github/workflows/ci.yml
on: push
jobs:
call-build:
uses: ./.github/workflows/build.yml
with:
node-version: '20'Inside a called workflow, github.event_name still shows the caller's event, such as push.
workflow_run: chaining workflows
workflow_run starts a workflow when another workflow requests or finishes a run. Name the upstream workflow by its name: value.
on:
workflow_run:
workflows: ["CI"]
types: [completed]
jobs:
report:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- run: echo "CI passed on ${{ github.event.workflow_run.head_branch }}"Note these rules:
completedfires for failed and cancelled runs too, so checkconclusion.- The workflow file is read from the default branch, so edits on a feature branch have no effect until they are merged.
- It runs with the repository's secrets and a write-capable token, even when the upstream run came from a fork. Never check out and run untrusted fork code in it.
- Chains are limited in depth, so do not build long pipelines this way.
repository_dispatch: events from outside
repository_dispatch lets an external system start a workflow through the REST API. The sender picks an event_type and can attach a client_payload.
on:
repository_dispatch:
types: [deploy-request]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- env:
TARGET: ${{ github.event.client_payload.env }}
run: echo "Deploy requested for $TARGET"Send the event with the GitHub CLI:
gh api repos/OWNER/REPO/dispatches \
-f event_type=deploy-request \
-f 'client_payload[env]=staging'As with workflow_run, the workflow file must be on the default branch. The caller needs a token with write access to the repository. Treat the payload as untrusted input and pass it through env, as above.
Comparison
| Event | Started by | Runs from | Typical use |
|---|---|---|---|
| `workflow_call` | A job in another workflow | The called file's ref | Shared build or deploy logic |
| `workflow_run` | Another workflow finishing | Default branch | Follow-up steps such as reports |
| `repository_dispatch` | An API request | Default branch | Chat bots, other services |
Exercise
A workflow named "CI" fails on a pull request. Your workflow_run workflow listening to "CI" with types: [completed] still starts and posts "success" to chat. What is the likely cause, and how do you fix it?
Show answer
completed fires for every finished run, whatever the result. Add if: github.event.workflow_run.conclusion == 'success' to the job so it only runs after a passing CI run.