Sub topic 1
Why overlapping runs are a problem
This chapter explains why GitHub Actions workflow runs overlap, what goes wrong when they do, and which problems the concurrency key solves. It sets up the vocabulary for the rest of the tutorial. It does not cover the syntax in detail.
Prerequisites
This chapter assumes you can already do the following:
- Read and write basic workflow syntax:
on,jobs,runs-on, andsteps. - Name the common triggers:
push,pull_request, andworkflow_dispatch. - Read an expression such as
${{ github.ref }}and know thatgithubis a context, meaning an object of data about the current run.
You do not need to know anything about the concurrency key yet.
What a workflow run is
A workflow is a YAML file in .github/workflows/. A workflow run is one execution of that file, started by an event. Each run gets its own ID, its own runners, and its own logs.
GitHub starts a new run for every matching event. It does not check whether an earlier run of the same workflow is still going. By default, runs are independent and execute in parallel, limited only by your plan's concurrency limits for runners.
That default is sensible for many workflows. Two unit-test runs on different commits do not interfere with each other. Problems start when runs share something, or when an older run's result no longer matters.
How runs come to overlap
Runs overlap whenever events arrive faster than runs finish. Common causes:
- Rapid pushes to a pull request branch. A developer pushes a commit, sees a typo, and pushes a fix 30 seconds later. Both pushes trigger the
pull_requestworkflow, and both runs are active at once. - Several merges to
mainin a short window. A team merges three pull requests in ten minutes. Each merge triggers thepushworkflow, which may include a deployment. - Manual and automatic triggers together. Someone starts a
workflow_dispatchrun while apushrun of the same workflow is still going. - Scheduled runs that outlast their interval. A
scheduleworkflow set to run every 15 minutes takes 20 minutes on a busy day, so the next run begins before the previous one ends. - Re-runs. Re-running a failed run from the UI creates activity next to whatever newer runs already exist.
The diagram shows the first case. Two pushes to the same branch produce two runs that execute side by side.
Problem 1: wasted compute and slow feedback
When run 1 tests commit A and run 2 tests commit B, the result of run 1 is usually irrelevant. Commit B contains everything in commit A plus the fix. Nobody needs to know whether commit A passed.
Run 1 still uses a runner and still bills minutes on private repositories. On a busy repository the cost adds up:
- A 10-minute workflow with 4 jobs uses 40 runner-minutes per run.
- If each pull request averages three pushes and the older runs are never needed, two thirds of that spend produces no useful information.
Wasted runs also slow everyone else. Runners are a limited resource. GitHub-hosted runners have a cap on simultaneous jobs per account, and self-hosted runners have a fixed fleet. Obsolete runs take slots that newer, relevant runs could use, so feedback on the current commit arrives later.
Problem 2: confusing results
Overlapping runs produce several status checks for one branch. The most recent push can finish first and the older run can finish last. If the older run fails because of a bug the newer commit already fixed, a red mark appears in the run list and can mislead reviewers who do not look at which commit the run tested.
Required status checks use the check for the head commit of the pull request, so branch protection is not usually fooled by a stale run. The confusion lands on humans, who see a list of runs, some red, some green, and have to work out which one counts.
Problem 3: races on shared resources
The most serious problem is not waste but correctness. Two runs that touch the same external resource at the same time can corrupt it or leave it in an unexpected state. Examples:
- Deployments. Two runs deploy to the same environment. If run 2 (commit B) finishes its deploy before run 1 (commit A), run 1 then overwrites the newer version with the older one. The environment ends up on the wrong commit even though every run reported success.
- Infrastructure tools. Terraform and similar tools hold a state lock. A second run that starts while the first holds the lock fails with a lock error, or worse, a tool without locking applies conflicting changes.
- Shared test environments. Integration tests that create and delete fixtures in one database or one staging environment can break each other's data.
- Package publishing. Two runs that try to publish the same version, or update the same release tag, can fail or leave a partial release.
- Writes back to the repository. Workflows that commit generated files can collide, and the second push is rejected because the branch moved.
The next diagram shows the deployment race. Both runs succeed, and the final state is wrong.
Seeing overlap in a real workflow
The following workflow has no concurrency control. It simulates a slow job and prints the commit it was triggered for. You can use it to observe overlap yourself.
name: Overlap demo
on:
push:
branches: [main]
workflow_dispatch:
jobs:
slow:
runs-on: ubuntu-latest
steps:
- name: Show which commit this run is for
run: echo "Run ${{ github.run_id }} is testing ${{ github.sha }}"
- name: Pretend to do slow work
run: sleep 120
- name: Report completion
run: echo "Run ${{ github.run_id }} finished"To observe the overlap:
- Commit this file to
.github/workflows/overlap-demo.ymlonmain. - Make two small commits to
mainwithin about a minute of each other. - Open the Actions tab. Both runs show as in progress at the same time, each with its own run ID.
Both runs proceed independently. Nothing in the workflow tells GitHub that they are related.
The idea behind the fix
The problems above have a common cause. GitHub does not know which runs belong together. The workflow author knows that two runs for the same pull request are related, or that every deployment to production competes for one resource. Concurrency control lets you state that knowledge.
You do this by giving related runs the same label, called a concurrency group. GitHub then enforces a simple rule for each group:
- At most one run (or job) in the group is running at a time.
- At most one more is pending, waiting its turn.
- Anything newer than that pending run replaces it. The replaced run is cancelled.
You can also tell GitHub to cancel the run that is already in progress when a newer one arrives. That option is cancel-in-progress. It fits tests and builds, where only the latest commit matters. For deployments you usually want the opposite: let the running deployment finish and queue the next one.
So the choice is not a single setting. It depends on what the runs share and whether a half-finished run is safe to stop.
| Situation | What goes wrong without control | Typical remedy |
|---|---|---|
| Many pushes to one pull request | Wasted minutes, stale results | Group by branch, cancel in-progress runs |
| Merges to `main` | Out-of-order deployments | Group per environment, do not cancel |
| Shared integration environment | Tests corrupt each other's data | Job-level group on the environment |
| Scheduled run longer than its interval | Runs pile up | Group per workflow |
Edge cases worth knowing now
- Concurrency is not locking inside a run. It limits runs and jobs against each other. It does not protect steps within one job.
- It is scoped to a repository. Groups in different repositories do not interact, even with the same name.
- Different workflows do not share a group by accident. A group name must match exactly. Two workflows only compete if you give them the same group string, a point that matters later when we look at naming collisions.
- Cancelling is not rolling back. Stopping a run that was halfway through a deployment leaves the half-finished state in place. Later chapters cover when it is safe to cancel and how to clean up.
- Not every overlap is a problem. Independent runs, such as tests on separate branches, are fine in parallel. Adding concurrency control where nothing is shared only slows feedback.
What comes next
The next chapter introduces the concurrency key itself: where it goes in a workflow file, what values it accepts, and how a group name is built.
Exercise
A workflow deploys to production on every push to main. Two merges happen two minutes apart. The second commit builds faster, so its deployment finishes first. Both runs report success. Which commit is running in production afterward, and why is this a correctness problem and not only a waste of minutes?
Show answer
The older commit is running. The first run's deploy step finishes last and overwrites the newer version. Both runs are green, so nothing signals the problem, and production is left on outdated code. Wasted minutes only cost money, but here the end state of a shared resource is wrong.
Exercise
Name two situations from this chapter where running workflow runs in parallel is harmless, and one where it is harmful. For the harmful one, name the shared resource.
Show answer
Harmless: unit tests on separate branches, or tests on different commits that use only their own runner. Harmful: two deployments to the same production environment, where the shared resource is the production environment (or its deployed version). Other valid answers include a shared staging database or a Terraform state.