GitHub Actions — Limit Concurrency and Cancel In-Progress Jobs

GitHub Actions allows you to group your workflows by a given identifier. Grouping workflows within the same group enqueues the new job in a pending state until a running job finishes. This limits concurrency to a single job for a given group. You can also tell GitHub Actions to cancel a given in-progress job of a group when new changes are pushed.

This tutorial shows you how to configure your GitHub Actions to group workflows and possibly cancel previous jobs if a new one is queued.

GitHub Actions Series Overview

Group GitHub Action Workflows/Jobs

GitHub Actions support job concurrency to ensure only a single job runs in the same concurrency group at the same time. A concurrency group is identified by a string value (name). You can compose the group identified using expressions, like creating a group from the branch name.

Here’s a sample workflow configuration providing a hard-coded group name:

name: Run tests

concurrency: group-name  

Grouping Workflows by Branch Name

You can also combine a static string value with a calculated value. This example prefixes the workflow with ci- and appends the branch name:

concurrency: ci-${{ github.ref }}  

Cancel In-Progress Workflows to Run the Newest Jobs

A common use case for workflow concurrency is to cancel in-progress jobs when pushing new changes. This will stop the current jobs for a workflow and run the GitHub Action against the latest code changes.

You need to adjust the concurrency configuration in your GitHub Actions workflow YAML file to cancel in-progress jobs. The concurrency configuration now has two children: the group identifier and a toggle whether to cancel-in-progress jobs:

name: Run tests

  group: ci-${{ github.ref }}
  cancel-in-progress: true

Enjoy limiting the concurrency of your jobs in GitHub Action!

Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.