Sub topic 1
Why Overlapping Runs Are a Problem
Prerequisites
This chapter assumes you know the following:
- Basic workflow syntax:
on,jobs, andsteps. - Common triggers such as
push,pull_request, andworkflow_dispatch. - The
${{ }}expression syntax and thegithubcontext.
You don't need to know the concurrency key yet. The next chapter introduces it.
Why runs overlap
A workflow run starts each time a trigger event fires. GitHub does not check whether an earlier run of the same workflow is still going. If you push three commits in a minute, you get three runs at the same time. Each run gets its own runner and its own copy of the code.
By default, GitHub Actions treats each run as independent. That default suits many workflows. It causes trouble in three common cases.
Wasted compute on outdated code
Suppose you open a pull request and push a fix a minute later. The run for the first commit is now outdated, because nobody cares about its result. It still occupies a runner until it finishes.
On a busy repository this adds up:
- Runner minutes are billed on private repositories, so outdated runs cost money.
- Runners are limited in number, so outdated runs delay the runs people are waiting for.
- The checks list on a pull request fills with stale results, which makes the current status harder to read.
Conflicting deployments
Overlap is worse when a workflow changes something outside the repository. Consider a deploy workflow that triggers on every push to main:
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Deploy to production"Two merges in quick succession start two deploys. Both write to the same environment. Each deploy takes a different amount of time, so the older commit's deploy can finish last. Production then ends up running older code than the newest commit on main. Deployments can also corrupt shared state if two of them run at once. Examples are a database migration that runs twice or a lock file that is half overwritten.
Contention for shared resources
Tests can conflict in the same way. Integration tests that use one shared staging environment, one test database, or one external API with a rate limit can fail when two runs use it at once. The failures look random. They depend on timing, not on your code, so they are hard to reproduce.
The three outcomes you may want
When two runs compete, you can choose what happens to the older one. Each choice fits a different situation:
| Situation | Desired behavior | Typical use |
|---|---|---|
| Older run's result no longer matters | Cancel the older run | Pull request checks |
| Older run must finish, but not alongside the new one | Make the new run wait | Production deploys |
| Runs don't interfere | Let them run in parallel | Independent builds |
The rest of this tutorial shows how to get the first two behaviors by using the concurrency key.
Common errors in thinking about overlap
- Assuming runs queue automatically. They don't. Without configuration, every run starts as soon as a runner is free.
- Assuming a newer push stops the older run. It doesn't. The older run continues to the end unless you cancel it.
- Assuming overlap only affects speed. For deployments and shared resources, overlap can produce wrong results.
Exercise
A workflow deploys to production on every push to main. Two commits are merged 30 seconds apart. Describe one way the default behavior can leave production in a wrong state.
Show answer
Both runs start at the same time and deploy independently. If the run for the older commit finishes after the run for the newer commit, it overwrites the newer deployment, so production ends up on the older commit.