Sub topic 3
Running on push
This chapter assumes you can read simple YAML, know what a Git branch, tag and commit are, and have seen a workflow file under .github/workflows/. It covers the push event and its basic filters. The next chapter goes deeper into filter syntax.
What the push event does
The push event fires when commits or tags are pushed to the repository. It also fires when a pull request is merged, because the merge creates a new commit on the base branch. The event is the simplest trigger and a common choice for continuous integration (CI) on your main branch.
Here is the smallest useful form:
name: CI
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Triggered by ${{ github.event_name }}"With no filters, this workflow runs for every push to every branch and for every pushed tag. GitHub reads the workflow file from the commit that was pushed. A change to the workflow file on a feature branch therefore takes effect on that branch right away.
Narrowing the trigger with filters
Running on every push is often wasteful. Use the map form of on to add filters:
on:
push:
branches:
- main
- 'release/**'
tags:
- 'v*'
paths-ignore:
- '**.md'This workflow runs for pushes to main, for pushes to any branch under release/, and for tags that start with v. It skips a push when every changed file is a Markdown file.
The patterns are globs, not regular expressions. * matches any characters except /. ** matches any characters including /. Quote any pattern that starts with * or contains characters YAML treats specially.
How the filters combine
The rules are easy to get wrong, so keep this table nearby.
| You define | Result |
|---|---|
| Only `branches` | Tag pushes never trigger the workflow. |
| Only `tags` | Branch pushes never trigger the workflow. |
| Both `branches` and `tags` | A push triggers the workflow if its ref matches either list. |
| `branches` and `paths` | A push must match both the branch and at least one changed path. |
| `paths` and `paths-ignore` | Not allowed together for the same event. |
Within one filter type, you can also use the -ignore variants (branches-ignore, tags-ignore). You cannot mix branches and branches-ignore for the same event. To exclude a branch while including others, use a ! pattern inside branches.
Common mistakes
- Listing only
branches: [main]and expecting release tags to trigger the run. They will not. - Writing
*.mdunquoted. YAML reads the leading*as an alias and fails to parse. - Using
paths-ignorewithpathsin the samepushblock. GitHub rejects this combination.
Exercise
A workflow has on: push: branches: [main]. You push the tag v1.2.0 from a commit that is on main. Does the workflow run? What one change would make it run?
Show answer
It does not run. Because only branches is defined, tag pushes do not match. Add a tags: ['v*'] filter next to branches, and a push of v1.2.0 will then trigger the workflow.