Sub topic 2
Triggering workflows with events
This chapter assumes you can read YAML, know what a branch and a pull request are, and have seen a basic workflow file. It covers the on key, which decides when a workflow starts.
How a trigger works
A workflow does nothing until an event happens in the repository. An event is something GitHub records, such as a push, a new pull request, a timer firing, or a manual click. When an event matches a workflow's on block, GitHub creates a workflow run.
Common events
The on key accepts a single event, a list, or a map with options for each event.
| Event | Fires when | Typical use |
|---|---|---|
| `push` | Commits are pushed to a branch or tag | Build and test after every push |
| `pull_request` | A pull request is opened, updated, or reopened | Check proposed changes |
| `schedule` | A cron time is reached | Nightly builds |
| `workflow_dispatch` | Someone starts it manually or through the API | On-demand tasks |
| `workflow_call` | Another workflow calls it | Reusable workflows (a later chapter) |
A workflow with several triggers
name: CI
on:
push:
branches: [main]
pull_request:
schedule:
- cron: '0 3 * * 1'
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Triggered by ${{ github.event_name }}"This workflow runs in four cases:
- Someone pushes to
main. - A pull request is opened or updated. With no options,
pull_requestuses its default activity types:opened,synchronize, andreopened. - Every Monday at 03:00 UTC, from the cron schedule.
- Someone clicks Run workflow in the Actions tab, which
workflow_dispatchenables.
The expression github.event_name is a context value. It tells you which event started the run. Contexts are covered in a later chapter.
Behavior to know
- Workflow files are read from the ref the event applies to. A push uses the file as it exists on the pushed branch. Most
pull_requestruns use the merge result of the pull request and its base branch. - Forks are restricted. For
pull_requestevents from a fork, secrets are not passed to the run andGITHUB_TOKENis read-only. This is deliberate, so untrusted code cannot read your secrets. - Schedules run only from the default branch, and they use UTC. Runs can start late when GitHub is busy.
workflow_dispatchonly appears in the UI once the file exists on the default branch.
Common mistakes
- Wrong indentation under
on.branchesmust sit under the event name, not beside it. - Expecting
scheduleto run on a feature branch. It will not. - Writing
on: [push]and then trying to add filters. The list form cannot take options. Switch to the map form.
Filtering by branch, path, and activity type is the next chapter.
Exercise
A workflow has on: [push, pull_request]. You push a commit to a branch that has an open pull request. How many workflow runs start, and why?
Show answer
Two. The push to the branch fires the push event, and updating the open pull request's branch fires pull_request (the synchronize activity). Each event creates its own run.
Exercise
You want a workflow that runs when a pull request is opened or updated, and also lets a teammate start it by hand. Write the on block.
Show answer
on: pullrequest: workflowdispatch: