Sub topic 2
Choosing when deployments run
This chapter assumes you can read a workflow file's top-level structure (name, on, jobs) and know what a branch and a pull request are. Here you decide which events start a workflow, and which of them are allowed to deploy.
The `on` key
The on key lists the events that start a workflow. Each event can carry filters. A run starts only when the event matches every filter you set.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:This block does three things:
pushwithbranches: [main]runs when commits land onmain. This includes the merge commit of a pull request.pull_requestruns on pull requests that target any branch. By default it fires when a pull request is opened, gets new commits, or is reopened.workflow_dispatchadds a "Run workflow" button in the Actions tab and allows manual starts through the API or the GitHub CLI.
A common pattern: test often, deploy rarely
Most teams want tests on every pull request, but a deployment only from main. A single workflow can do both if you gate the deploy job.
name: CI and deploy
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Run tests here"
deploy:
needs: test
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying ${{ github.sha }}"On a pull request, github.ref is a merge ref such as refs/pull/12/merge, so the condition is false and the deploy job is skipped. On a push to main, or a manual run started on main, it passes. A manual run started on another branch is also skipped.
Narrowing with filters
Filters keep deployments from running when nothing relevant changed.
| Filter | Example | Effect |
|---|---|---|
| `branches` | `branches: [main, 'release/**']` | Matches branch names, including glob patterns |
| `tags` | `tags: ['v*']` | Runs when a matching tag is pushed |
| `paths` | `paths: ['src/**', 'package.json']` | Runs only if a changed file matches |
| `paths-ignore` | `paths-ignore: ['**.md']` | Skips runs that touch only matching files |
Tag triggers suit release-style deployments: push v1.4.0 and the workflow ships that version. Use branches and tags together under push and either one can start a run.
Common errors
- Deploying from
pull_request. Pull requests can come from forks or contain unreviewed code. Fork-triggered runs do not receive your secrets, and you should not run deployment steps for them. - A required check that never reports. If a
pathsfilter prevents the workflow from running, a branch protection rule that requires that check can wait forever. Keep required checks unfiltered. workflow_dispatchbutton missing. The workflow file must exist on the repository's default branch for the button to appear.- Quoting patterns. In YAML, a value starting with
*needs quotes, for example'**.md'.
Exercise
You want tests on every pull request, but a deployment only when main changes and only if files under src/ or package.json changed. Which trigger block and job condition would you use?
Show answer
Use on: pull_request: plus push: branches: [main] with paths: ['src/**', 'package.json']. Then give the deploy job if: github.event_name == 'push'. Pull requests run tests only. Pushes to main that touch those paths also deploy. Note that the paths filter on push does not affect the pull_request trigger, so tests still run on every pull request.