A workflow does nothing until something starts it. The on key in the workflow file lists those events, called triggers.
Push and pull request
on:
push:
branches: [main]
pull_request:
branches: [main]This runs the workflow when commits are pushed to main, and when a pull request that targets main is opened or updated. Note the difference: under pull_request, branches refers to the target (base) branch, not the branch you are working on.
A push to a feature branch with no pull request does not match here.
Filtering by path
You can skip runs that cannot matter:
on:
push:
paths:
- "src/**"
- "package.json"The workflow runs only if the push changes at least one matching file. Be careful when a check is required before merging: a workflow skipped by a path filter can leave that required check waiting.
Manual and scheduled runs
on:
workflow_dispatch:
schedule:
- cron: "30 2 * * 1"workflow_dispatch lets you start a run by hand from the Actions tab or with the GitHub CLI:
gh workflow run ci.ymlFor the manual button to appear, the workflow file must be on the default branch.
schedule uses cron syntax with five fields: minute, hour, day of month, month and day of week. 30 2 * * 1 means 02:30 every Monday. GitHub reads these times as UTC by default. Scheduled runs use the latest commit on the default branch and can start late when the service is busy.
Checking which event ran
A step can print the event name, which helps while you learn:
- run: echo "Started by $GITHUB_EVENT_NAME on $GITHUB_REF"Started by push on refs/heads/mainIf a workflow does not start, check that the file is in .github/workflows/, that the YAML is valid, and that your branch and paths match the filters. Indentation errors are common.
The official Actions documentation lists every event and filter.
Try it
Add workflow_dispatch and a push filter for main to a practice repository. Push to another branch and confirm nothing runs, then start it by hand. Practise the cron fields on the command line too, in the processes and cron lab.
Check your understanding
Workflow Triggers
All 3 sections · no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.