Run a GitHub Actions workflow manually with workflow_dispatch

CI in my private repos ran twice for every change, once on the pull request and again on main after the merge, and GitHub counts Actions minutes in private repos against a monthly quota. I took the push trigger out and added workflow_dispatch, which lets me start the workflow by hand, so I can still run CI on main when I want to:

on:
  pull_request:
  workflow_dispatch:   # works alongside any other trigger; here it replaced push: branches: [main]

A manual run only works once the workflow file with workflow_dispatch is on the default branch (main here). After that, open the repo’s Actions tab, pick the workflow, click Run workflow above the list of runs, and choose main as the branch (GitHub’s docs). The GitHub CLI (gh) does the same from a terminal:

gh workflow run test.yml --ref main   # test.yml is the file in .github/workflows/, --ref the branch to run on

Dropping the run on main loses little test coverage, because a pull_request run tests a trial merge of the branch into main as main stood at that run, not the branch alone. The exception is two pull requests that each pass on their own and break main once both merge, which nothing catches until the next pull request’s CI runs. A manual run on main is the check for that after a few pull requests merge close together, or when a pull request fails and it isn’t clear whether main was already broken. GitHub’s merge queue catches that case before the merge, but it isn’t available for repos on a personal account.