All guides

Concurrency groups: stop two deploys from running at once

By default GitHub Actions lets several runs of the same workflow run at the same time, so two pushes can start two deploys. A concurrency group lets one run at a time proceed, and you choose whether the others wait, get replaced or get canceled.

A laptop screen showing JavaScript in a code editor

What a concurrency group does

The concurrency key puts a workflow or a job in a named group. Within a repository, at most one job or workflow run in a group is running at any time.

YAML

concurrency:
  group: production-deploy

The name is any string or expression. It is case insensitive, so prod and Prod are the same group. Group names are shared across the workflows of a repository: two workflows that use the same name are in one group.

What happens to a pending run

The default is not a queue. Take three runs in one group:

  1. Run A is in progress. Run B is queued in the same group and becomes pending.
  2. Run C is queued. GitHub cancels B, and C takes its place as the pending run.
  3. A is not canceled. Only the pending run was replaced.

By default a group has at most one running and one pending run. Two options change this:

  • cancel-in-progress: true also cancels the run that is in progress. In the example, B cancels A.
  • queue: max lets up to 100 jobs or runs be pending in the group. When the queue is full, additional ones are canceled. The default value is queue: single.

queue: max and cancel-in-progress: true cannot be combined. GitHub rejects the workflow with a validation error.

Pending runs are processed first in, first out, by the time each one started waiting on the group. GitHub adds that the order is not guaranteed, because the actual start time of a run varies.

Outdated pull request runs

GitHub's own example of this use is canceling linters that check outdated commits. For pull requests:

YAML

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

github.head_ref is the source branch of a pull request, and it is defined only on pull request events. On any other event the expression falls back to github.run_id, which is unique for the run, so the group cancels in-progress runs on pull request events only.

github.workflow belongs in the name. With several workflows in a repository, group names must be unique across workflows, or a run cancels in-progress runs of the other workflows.

A production deploy that is never canceled

A run that is in progress is canceled only when cancel-in-progress is true. For a deploy job, leave the option out. This is GitHub's deployment example with a job-level group:

YAML

jobs:
  deployment:
    runs-on: ubuntu-latest
    environment: production
    concurrency: production
    steps:
      - uses: actions/checkout@v7
      - run: ./deploy.sh

A second deploy waits as pending while the first one runs, and a third one cancels the pending second. GitHub describes the result as a maximum of one running and one pending job in the group. ./deploy.sh stands for your own deploy steps.

If every run has to execute, in sequence and with none canceled, opt in to queuing:

YAML

concurrency:
  group: production-deploy
  queue: max

Workflow level versus job level

At the workflow level, the group covers the whole run. The group expression can use the github, inputs and vars contexts.

At the job level, only that job is held as pending, and the other jobs in the workflow proceed. The expression can also use the needs, strategy and matrix contexts.

cancel-in-progress accepts an expression too. GitHub's example cancels in-progress runs on every branch that is not a release branch:

YAML

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ !contains(github.ref, 'release/')}}

Pushes to a release/1.2.3 branch do not cancel in-progress runs. Pushes to another branch, such as main, do.

Mistakes to avoid

  1. Expecting a queue by default. One run can be pending. A newer run replaces it unless the group sets queue: max.
  2. One group name in two workflows. Put github.workflow in the name unless the workflows are meant to share the group.
  3. Expecting the environment to limit runs. GitHub states that concurrency and environment are not connected. A workflow that uses the same environment and does not specify concurrency is not subject to any concurrency rules. Give every workflow that deploys to the environment the same group name.
  4. The same group in a reusable workflow and its caller. A called workflow has its caller's name in github.workflow. If both use it as the group with cancel-in-progress: true, the caller is canceled when the called workflow runs.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides