Sub topic 8
Configuring the three triggers
This chapter shows how to wire pull request, merge, and nightly stages to the events that start them in GitHub Actions. It assumes you know the basics of Git and pull requests, can read a YAML workflow file, and know what linting, unit tests, and integration tests are. The ideas carry over to GitLab CI and Jenkins, but the examples use GitHub Actions syntax.
What a trigger is
A trigger is the event that starts a workflow run. In GitHub Actions, the on: key lists the events. Each event produces a run with its own commit, its own context, and its own status check.
The three stages in this tutorial map to three kinds of events:
| Stage | Event | Commit that is tested | Typical contents |
|---|---|---|---|
| Pull request | `pull_request` | A merge of the PR branch into the base branch | Lint, unit tests, build |
| Merge | `merge_group` and `push` to `main` | The queue's candidate commit, or the new tip of `main` | Integration tests |
| Nightly | `schedule` and `workflow_dispatch` | The tip of the default branch | End-to-end suite |
Keeping one workflow file per stage makes each file short. It also gives each stage a clear status check name, which matters when you configure required checks later.
Trigger 1: pull requests
The pull request workflow gives feedback to the author while the change is still fresh. It should finish in minutes, so it holds only fast checks.
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 testThree details matter here.
branches: [main]filters on the base branch. Forpull_request,branchesmatches the branch the PR targets, not the branch the PR comes from. A PR intorelease/1.0will not run this workflow.- The default activity types are
opened,synchronize, andreopened. The workflow runs when a PR is opened, when new commits are pushed to it, and when it is reopened. Edits to the title or labels do not trigger it. You can add types such asready_for_reviewif you skip draft PRs. - The checked-out commit is a test merge. For
pull_request,actions/checkoutby default checks out a temporary merge of the PR head into the base branch. This means the PR is tested against the current base, which catches some conflicts early. It also means the SHA you see in the run differs from the SHA of your last commit.
Fork pull requests
When a PR comes from a fork, the workflow runs with a read-only token and without repository secrets. This is a security measure. Keep the pull request stage free of checks that need secrets, such as deploying to a cloud account. If a check needs secrets, it belongs at merge time or later.
Trigger 2: merge queue and pushes to main
The merge stage tests what is about to become main, or what just became main. It can afford slower checks because it runs far less often than the PR stage.
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:integrationThe two events do different jobs.
merge_groupfires when a merge queue builds a candidate commit. The queue combines the PR with the current tip ofmainand any PRs ahead of it. If this run passes, the queue merges the PR. If it fails, the queue removes the PR. Because the check runs before the merge, a broken combination never reachesmain.pushtomainfires after a merge has happened. It is a safety net. It also covers repositories that do not use a merge queue, and it covers direct pushes by administrators.
You need both when you use a merge queue and also want a post-merge record. If you have no merge queue, push alone is enough, but a failure then means main is already broken, and someone has to fix or revert it.
Merge queue requirements
A merge queue only works when the repository enables it in a branch protection rule or ruleset. The required checks must also run on merge_group. If a required check is defined only for pull_request, the queue waits for a status that never arrives, and the PR sits in the queue until it times out.
If you want the fast checks to run again in the queue, add the event to the PR workflow:
on:
pull_request:
branches: [main]
merge_group:Note that merge_group does not accept branches filters in the same way. Branch protection decides which queue applies.
Trigger 3: scheduled and manual runs
The nightly workflow runs the slowest suite on a clock instead of on a code event.
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:e2ePoints to know about schedules:
- Cron uses UTC.
0 2 * * *means 02:00 UTC every day. Convert from your local time zone yourself. - Scheduled runs use the default branch. The workflow file and the code come from the latest commit on the default branch. You cannot schedule a run on a feature branch.
- Runs can start late. Runs at the top of the hour often queue behind other scheduled jobs. Choosing a minute such as
17 2 * * *avoids the busiest moment. Do not rely on exact start times. - Schedules can be disabled. In public repositories, GitHub disables scheduled workflows after 60 days without repository activity. You get a notice, and you can re-enable the workflow.
- Set
timeout-minutes. The default job timeout is 360 minutes. A hung end-to-end run would otherwise burn six hours of runner time. The example uses 120.
workflow_dispatch adds a "Run workflow" button and an API entry point. Use it to rerun the nightly suite after you fix a failure, without waiting a day. It can also take inputs, such as a test subset:
on:
workflow_dispatch:
inputs:
grep:
description: Only run tests matching this text
required: false
default: ''You then read the value with ${{ inputs.grep }} in a step.
Prevent duplicate and stale runs
Pushing three commits to a PR in quick succession starts three runs. Only the last one matters. A concurrency group cancels the older ones:
concurrency:
group: pr-${{ github.event.pull_request.number }}
cancel-in-progress: trueUse this on PR workflows. Do not use cancel-in-progress: true on push to main or on merge_group, because every commit on main should keep its own result. For the nightly workflow, a fixed group name with cancel-in-progress: false stops a manual rerun from overlapping a scheduled run.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| PR into `release/*` shows no checks | `branches: [main]` filters on the base branch | Add the release branch patterns |
| PR waits forever in the merge queue | Required check lacks a `merge_group` trigger | Add `merge_group:` to the workflow |
| Nightly never starts | Workflow file is not on the default branch, or schedule was disabled | Merge the file to the default branch and re-enable |
| Nightly starts at 03:00 in summer | Cron is UTC, local time shifts with daylight saving | Reason in UTC or adjust the cron |
| Fork PR fails on a secret | Secrets are withheld from fork PRs | Move the check to merge time |
| Integration tests run twice per merge | Both `merge_group` and `push` run them | Accept it as a post-merge record, or drop `push` if the queue is mandatory |
Choosing triggers for a new check
When you place a new check, ask three questions in order.
- Does the author need the result before asking for review? If yes, use
pull_request. - Does it need to prove the combined result of several changes, or need secrets? If yes, use
merge_groupandpushtomain. - Is it too slow or too flaky to gate a merge? If yes, use
schedulewithworkflow_dispatch.
Exercise
Your team uses a merge queue. The pr workflow runs lint and unit tests on pull_request and is marked as a required check. PRs enter the queue and then stay there until they time out. What is wrong, and what is the smallest change that fixes it?
Show answer
The required check is only triggered by pull_request, so the queue's candidate commit never produces that status. Add merge_group: to the on: block of the pr workflow. The workflow then runs on the queue's candidate commit, reports the required status, and the queue can merge the PR.
Exercise
A nightly workflow with cron: '0 2 * * *' is meant to run at 02:00 local time in a UTC+2 time zone, but it runs at 04:00 local time. Why, and how would you change the cron to get 02:00 local time?
Show answer
Cron schedules in GitHub Actions use UTC, so 02:00 UTC is 04:00 at UTC+2. To run at 02:00 local time, use 00:00 UTC: cron: '0 0 * * *'. If the time zone observes daylight saving, the local time will shift by an hour for part of the year.