Sub topic 8
Configuring the three triggers
This chapter assumes you know basic GitHub Actions syntax (workflows, jobs, steps) and the pull request workflow. The examples use GitHub Actions. GitLab CI and Jenkins have equivalents, such as merge request pipelines and scheduled pipelines.
Mapping stages to events
Each stage in the pipeline corresponds to one workflow event. The on: block of a workflow file decides which stage the workflow belongs to.
| Stage | Event | Typical checks |
|---|---|---|
| Pull request | `pull_request` | Lint, unit tests |
| Merge | `merge_group`, `push` to `main` | Integration tests |
| Nightly | `schedule`, `workflow_dispatch` | End-to-end suite |
Pull request trigger
The pull_request event fires when a pull request is opened, when new commits are pushed to it, and when it is reopened. The branches filter matches the branch the pull request targets, not the branch it comes from.
name: pr
on:
pull_request:
branches: [main]
jobs:
fast-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm testMerge trigger
A merge queue builds a temporary candidate commit that combines your change with the latest main and everything queued ahead of it. It emits the merge_group event for that commit. Pairing it with push on main also covers repositories that do not use a queue.
name: merge
on:
merge_group:
push:
branches: [main]
jobs:
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run test:integrationNightly trigger
The schedule event takes a cron expression. The five fields are minute, hour, day of month, month, and day of week. GitHub evaluates them in UTC, so 0 2 * * * means 02:00 UTC every day. Adding workflow_dispatch lets someone start the run by hand, for example to rerun after a fix.
name: nightly
on:
schedule:
- cron: '0 2 * * *'
workflow_dispatch:
jobs:
e2e:
runs-on: ubuntu-latest
timeout-minutes: 120
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run test:e2eCommon errors
- Schedule never fires. Scheduled workflows run only from the default branch. A workflow file that exists only on a feature branch will not run on schedule.
- Manual run is missing from the UI. The "Run workflow" button also reads the workflow file from the default branch.
- Start time drifts. Scheduled runs can start several minutes late when GitHub is busy. Do not depend on the exact minute.
- Schedule silently stops. In public repositories, GitHub disables scheduled workflows after 60 days without repository activity. Check for this if nightly results stop appearing.
- No timeout. A hung end-to-end job holds a runner until the platform default of 360 minutes. Set
timeout-minuteson slow jobs, as the nightly example does. - Duplicate runs. If the same workflow listens to both
pull_requestandpushonmain, a change can be tested twice. Keep fast checks onpull_requestand slower ones onmerge_groupandpush.
Exercise
Your team added a required job integration to the merge workflow, but the merge queue has been "waiting for status" for an hour. The workflow's on: block lists only push with branches: [main]. What is wrong, and what is the fix?
Show answer
Queue candidates trigger the merge_group event, and the workflow does not listen for it. The required check never runs on the candidate commit, so it never reports a status. Add merge_group: to the on: block, so the job runs on queue candidates and the queue can proceed.
Exercise
A nightly workflow with cron: '0 2 * * *' exists only on the branch feature/e2e. Why does it never run, and how do you start it manually once it works?
Show answer
Scheduled workflows run only from the default branch, so the file must be merged into main first. Once it is there and includes workflow_dispatch, you can start it from the Actions tab or with gh workflow run nightly.