Triggers: push, pull_request & workflow_dispatch
The on: key decides when a workflow runs. Three triggers cover most pipelines: push for branches and tags, pull_request for review, and workflow_dispatch for a manual "Run workflow" button. Branch, tag and path filters use glob patterns; how they match (and why release/* doesn't match release/v2/x) is modelled in Release Engineering: Workflows & Triggers.
push
on:
push:
branches: [main, 'release/**']
tags: ['v*']
paths-ignore: ['docs/**', '**.md']- Runs on the pushed commit (
github.sha), with full access to the repository's secrets. branchesandtagsare separate filters: with onlybranches:given, tag pushes don't run the workflow, and vice versa.- Path filters are ignored for tag pushes, and a push is skipped only if every changed file matches
paths-ignore.
pull_request
on:
pull_request:
branches: [main] # the PR's *base* branch
types: [opened, synchronize, reopened] # the default activity types- Runs on a merge commit of the PR head into the base (
refs/pull/<n>/merge), so you test what the merge would produce. - The default types are
opened,synchronize(new commits pushed) andreopened. Addready_for_reviewif you skip drafts withif: github.event.pull_request.draft == false, orlabeledfor label-driven jobs. - PRs from forks get a read-only
GITHUB_TOKENand no secrets (exceptGITHUB_TOKEN). That is a deliberate protection: a stranger's PR can change the workflow file. First-time contributors' runs also wait for a maintainer's approval by default. pull_request_targetruns in the context of the base branch with secrets and a write token, for automation such as labelling. Never check out and execute the PR's code in it (Hardening Workflows).
workflow_dispatch
A manual trigger with a form. Inputs have types string, boolean, choice, number and environment:
on:
workflow_dispatch:
inputs:
target:
description: Where to deploy
type: environment # a dropdown of the repository's environments
required: true
version:
description: Image tag to deploy (e.g. 1.4.0)
type: string
required: true
dry-run:
type: boolean
default: true
jobs:
deploy:
runs-on: ubuntu-24.04
environment: ${{ inputs.target }}
steps:
- run: ./ci/deploy.sh "$VERSION" "$DRY_RUN"
env:
VERSION: ${{ inputs.version }} # inputs are untrusted text too: pass them via env
DRY_RUN: ${{ inputs.dry-run }}From the command line (sample session, needs a GitHub login):
gh workflow run deploy.yml -f target=staging -f version=1.4.0 -f dry-run=false
gh run list --workflow deploy.yml --limit 1✓ Created workflow_dispatch event for deploy.yml at main
To see runs for this workflow, try: gh run list --workflow="deploy.yml"
STATUS TITLE WORKFLOW BRANCH EVENT ID ELAPSED AGE
* deploy deploy main workflow_dispatch 18421907733 4s about 4 seconds ago
The run uses the workflow file from the branch you choose (--ref, default: the default branch). Only users with write access can dispatch.
Other triggers worth knowing
| Trigger | Use |
|---|---|
schedule (cron: '17 3 * * *', UTC) |
nightly scans, dependency checks; pick an odd minute, top-of-the-hour runs are delayed when GitHub is busy |
workflow_call |
makes the workflow reusable (Reusable Workflows) |
workflow_run |
run after another workflow completes (e.g. deploy after ci succeeded on main) |
merge_group |
runs for the merge queue; required checks must also listen to this event or the queue waits forever |
release, repository_dispatch |
publish after a GitHub Release; trigger from an external system through the API |
A model: which workflows run?
A repository usually has several workflow files. The model holds a simplified version of their on: blocks and decides, for a few events, which ones start (branch filters as plain globs; the full matching rules are in the release-engineering model linked above):
from fnmatch import fnmatchcase
WORKFLOWS = {
"ci.yml": {"push": {"branches": ["main"]}, "pull_request": {"branches": ["main"]}},
"release.yml": {"push": {"tags": ["v*"]}},
"deploy.yml": {"workflow_dispatch": {}, "workflow_run": {"workflows": ["ci"], "branches": ["main"]}},
"nightly.yml": {"schedule": {}},
}
DEFAULT_PR_TYPES = {"opened", "synchronize", "reopened"}
def triggered(spec, event):
kind = event["kind"]
if kind not in spec:
return False
f = spec[kind]
if kind == "push":
ref = event["ref"]
if ref.startswith("refs/tags/"):
return any(fnmatchcase(ref[10:], p) for p in f.get("tags", []))
return any(fnmatchcase(ref[11:], p) for p in f.get("branches", []))
if kind == "pull_request":
return event["action"] in f.get("types", DEFAULT_PR_TYPES) and \
any(fnmatchcase(event["base"], p) for p in f.get("branches", ["*"]))
if kind == "workflow_run":
return event["workflow"] in f["workflows"] and event["branch"] in f["branches"]
return True
events = [
{"kind": "push", "ref": "refs/heads/main"},
{"kind": "push", "ref": "refs/heads/feature/cart"},
{"kind": "push", "ref": "refs/tags/v1.4.0"},
{"kind": "pull_request", "action": "opened", "base": "main"},
{"kind": "pull_request", "action": "labeled", "base": "main"},
{"kind": "workflow_run", "workflow": "ci", "branch": "main"},
{"kind": "workflow_dispatch"},
]
for e in events:
label = " ".join(str(v) for v in e.values())
runs = [name for name, spec in WORKFLOWS.items() if triggered(spec, e)]
print(f"{label:<38} -> {', '.join(runs) or 'nothing'}")push refs/heads/main -> ci.yml
push refs/heads/feature/cart -> nothing
push refs/tags/v1.4.0 -> release.yml
pull_request opened main -> ci.yml
pull_request labeled main -> nothing
workflow_run ci main -> deploy.yml
workflow_dispatch -> deploy.yml
Two lines deserve attention: a push to a feature branch runs nothing here (CI comes from the PR instead, which avoids running it twice), and a labeled event doesn't run CI, because it isn't one of the default activity types.
Test yourself
A workflow has on: push: branches: [main]. Does pushing tag v1.0.0 run it?
- Yes, tags are on main
- No: with only
branchesgiven, tag pushes don't trigger it - Only if the tag is annotated
- Only with paths
Why can't a pull_request workflow from a fork read repository secrets?
- A bug
- The fork's author controls the workflow file in their PR; secrets would be exposed to anyone
- Forks have their own secrets
- It can, with approval
Which workflow_dispatch input type shows a dropdown of the repository's environments?
- choice
- environment
- string
- boolean