Sub topic 2
Writing the on key
This chapter covers the on key, which connects events to workflow runs. It assumes you can read simple YAML and have used branches, commits, and pull requests on GitHub. It also assumes you know what continuous integration is.
What the on key does
Every workflow file in .github/workflows/ needs an on key. The key lists the events that start the workflow. An event is something that happens in or around your repository, such as a push, a new pull request, a timer firing, or a person clicking a button.
When an event happens, GitHub checks the workflow files on the relevant commit. Each workflow whose on key matches the event gets a new run. A workflow with no matching event does nothing.
The three forms
The on key accepts three shapes. They describe the same thing at different levels of detail.
Single event
Use a plain string when one event is enough and you need no options.
name: CI
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Triggered by ${{ github.event_name }}"List of events
Use a list for several events that need no options.
on: [push, pull_request]Map of events
Use a map when an event needs options such as filters or activity types. Each event name becomes a key, and its settings are nested below it.
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:An event with no options, like workflow_dispatch: above, has an empty value. This is valid YAML and means "use the defaults".
The following table shows when each form fits.
| Form | Example | Use when |
|---|---|---|
| Single | `on: push` | One event, default behavior |
| List | `on: [push, pull_request]` | Several events, default behavior |
| Map | `on:` with nested keys | Any event needs filters, types, or inputs |
Common errors
- Switching forms mid-edit. If you write
on: pushand then addbranches:below it, the file is invalid. Change the value to a map first: putpush:on its own line, then indent the options under it. - Wrong event names. Names are lowercase with underscores, such as
pull_requestandworkflow_dispatch. A misspelled name is not an event, so the workflow never runs. - Quoting
on. Some YAML 1.1 parsers read an unquotedonas the booleantrue. GitHub handles this correctly, but other YAML tools and linters may complain. This is a tooling issue and does not affect your workflow on GitHub. - Expecting events from a branch you have not pushed. GitHub reads the workflow file from the commit that the event refers to. For a push, that is the pushed commit. A file that exists only on another branch does not respond to your event.
Exercise
You want a workflow to run on every push and on every pull request, with no filters. Which forms of the on key can express this, and which form do you need if you later want to limit pushes to the main branch?
Show answer
Both the list form (on: [push, pull_request]) and the map form can express it. Once you add a branches filter to push, you need the map form, with push: and branches: [main] nested under it.