Reducing Duplicate CI Runs with GitHub Actions Concurrency `cancel-in-progress`
Reducing Duplicate CI Runs with GitHub Actions Concurrency cancel-in-progress TL;DR: I added a concurrency block with cancel-in-progress: true to the ci-e2e.yml workflow to automatically abort older runs on the same branch when a new push arrives. This change cuts down on wasted minutes and prevents flaky test artifacts caused by overlapping executions. The Problem Our tvview project runs an…
Duplicate CI runs can waste valuable minutes and lead to flaky test results when GitHub Actions queues multiple workflow executions for the same branch. In a TV view project that runs an end-to-end test suite on every push and a scheduled cron job, this issue became apparent when developers pushed multiple commits in quick succession.
GitHub Actions would queue a new workflow run for each commit, resulting in duplicate work that was eventually superseded by the latest commit. This caused wasted CI minutes and potential race conditions due to overlapping executions.
The reporter initially tried to mitigate the issue by adding a manual "skip if already running" step at the beginning of the job using the GitHub CLI (gh). While this approach worked locally, it introduced race conditions and extra API calls that consumed rate-limited requests. The script also added unnecessary complexity, prompting the reporter to look for a built-in solution.
GitHub Actions introduced a concurrency feature that allows grouping runs by a key and optionally cancelling older runs when a new one starts. By adding a concurrency block with cancel-in-progress: true to the ci-e2e.yml workflow, the reporter was able to automatically abort older runs on the same branch when a new push arrived. This single-line change resolved the issue without adding any custom scripting.
To decide on an appropriate concurrency key, the reporter chose ${{ github.ref }} because it resolves to the full Git ref (e.g., `refs/heads/main` or `refs/pull/12/merge`). This ensures that only runs on the same branch or pull request share a group and can be cancelled accordingly. The updated workflow file included the concurrency block at the top level, which applied to all jobs within the workflow, automatically propagating the cancellation behavior.
After implementing the concurrency feature, the reporter measured the savings over a week and found that total CI minutes decreased from 560 to 210, duplicate runs dropped from 12 to 0, and the cost reduction amounted to approximately $3.50. While the reduction may seem modest, it represents a meaningful improvement for a project that runs heavy end-to-end tests.
The reporter also suggested a potential next step: adding a matrix strategy to run the same E2E suite against multiple Node versions in parallel while still keeping the concurrency guard at the workflow level. This would allow for broader compatibility coverage without reintroducing duplicate runs.
In summary, by leveraging GitHub Actions' concurrency feature with cancel-in-progress: true, the reporter successfully reduced duplicate CI runs, improved efficiency, and prevented flaky test artifacts caused by overlapping executions.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.