Sub topic 10
Running less: trigger filters and concurrency
The fastest job is the one that never starts. This chapter covers two ways to cut work: trigger filters that skip runs that can't change the result, and concurrency settings that cancel runs that a newer commit has replaced. It assumes you can read a workflow file, know what on: does, and have opened the Actions UI to look at run durations.
Why running less counts as a speed fix
Caching and parallelism make a run shorter. Trigger scoping and concurrency remove runs altogether. Both also reduce queue time for everyone else, because every skipped run frees a runner for another job.
Measure first. If your run list shows many runs for documentation-only changes, or several runs of the same pull request stacked up behind each other, these fixes will pay off. If it doesn't, leave them alone.
Path filters
A paths filter starts the workflow only when a changed file matches one of the patterns.
name: ci
on:
pull_request:
paths:
- "src/**"
- "package.json"
- "package-lock.json"
- ".github/workflows/ci.yml"Include the workflow file itself, so edits to the pipeline are tested by the pipeline. Use paths-ignore when it is easier to list what to skip, such as docs/** or **.md. You can't combine paths and paths-ignore for the same event. To mix both, use paths with negated patterns that start with !.
Branch filters work the same way. branches limits a push trigger to the branches you list, so feature branches that already have a pull request don't run twice.
on:
push:
branches: [main]
pull_request:With this trigger block, a commit to a pull request branch runs once (as a pull request event) and a merge to main runs once (as a push event).
Cancelling superseded runs
When you push three commits in a row, you get three runs. Only the last one matters. A concurrency block groups runs and lets a new run cancel the old one.
name: ci
on:
pull_request:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testThe group is a string. Runs with the same string share one slot. Here the string includes github.ref, so each pull request has its own group and pushes to one pull request never cancel runs on another.
Without cancel-in-progress, GitHub keeps one running and one pending run per group. A newer pending run replaces an older pending one, but the running one finishes. That alone saves some work. Adding cancel-in-progress: true stops the running one too.
When not to cancel
Cancelling is wrong for runs whose side effects must complete, such as deployments or release publishing. A cancelled deploy can leave an environment half updated. Cancel only on pull request runs and let runs on main queue:
concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}cancel-in-progress accepts an expression, so one file can use both behaviors.
How the decision flows
Checking the effect
Cancelled runs show a grey "Cancelled" status in the run list. Compare the number of runs and total runner minutes per week before and after the change. A cancelled run also still counts the minutes it used before it stopped, so the saving shows up as shorter runs, not as zero.
Common mistakes
- A group name that is too broad, such as
group: ci, makes unrelated pull requests cancel each other. - A group name that is too narrow, such as one containing
github.sha, never matches an older run, so nothing is cancelled. - Path filters on a required check block merges.
- Filtering by path when a changed file outside the list (a shared config or lockfile) still affects the build.
Exercise
Your workflow runs on both push and pull_request for every branch, and each commit to a pull request branch starts two runs. Name two changes that reduce this, and say what risk each carries.
Show answer
First, limit push to branches: [main], so pull request branches run only through the pull_request event. The risk is that branches without an open pull request get no CI. Second, add a concurrency group keyed on github.ref with cancel-in-progress: true, so a newer run cancels the older one on the same ref. The risk is cancelling a run whose side effects, such as a deploy, must finish, so restrict cancellation to pull request events if the workflow deploys.
Exercise
A required check named test uses paths: ["src/**"]. A pull request that only edits README.md can't be merged. Why, and how can you fix it?
Show answer
The workflow never starts, so the test check is never reported and stays "Expected". Fix it by removing the path filter from the required workflow and skipping work inside it (for example with a job-level if based on changed files), so the check always reports a result.