How to Cancel Running Workflows and Only Run the Latest with GitHub Actions
Introduction
Let’s say you have a GitHub Actions workflow set up under the following conditions:
- A workflow deploys changes to the Staging environment whenever there’s a merge into the
develop
branch - There’s only one Staging environment
- You want to conserve your GitHub Actions usage quota as much as possible
In such scenarios, it’s often desirable to cancel any currently running workflows and only run the most recent one. GitHub Actions provides a feature that allows this behavior, and this post documents how to configure it.
Configuration
You can achieve this by setting cancel-in-progress
to true
within the concurrency
key. The concurrency
field allows you to control the parallel execution of workflows or jobs.
on:
push:
branches:
- develop
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
The group
field controls parallel execution within the same group name. In the example above, using deploy-${{ github.ref }}
as the group name allows you to manage concurrency per branch.
You can also set concurrency
at the job level:
jobs:
some-job:
runs-on: ubuntu-latest
concurrency:
group: job-something
cancel-in-progress: true
Conclusion
With this setup, you can avoid unnecessary workflow runs and save on your GitHub Actions usage quota.
For more details on concurrency
, refer to the official documentation: