Concurrency cancel-in-progress but not when ref is master

My workflow runs “on push” and has the following concurrency field:

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

This works as expected when pushing multiple times from the same branch.
The problem is that when I merge 2 different PR’s then since github.ref is master in that case it will cancel one of them

is there a way to add a conditional? or a ‘to ignore’? to the concurrency block?

4 Likes

I also need the same.

The closes I have found is the following. Then you avoid the canceling, but master builds will run in sequence which is a big problem for us as there is not enough time in the day for that.

concurrency:
  group: ${{github.ref}}
  cancel-in-progress: ${{github.ref != 'refs/heads/master'}}

What is really needed is support for this:

concurrency:
  if: ${{github.ref != 'refs/heads/master'}}
  group: ${{github.ref}}
  cancel-in-progress: true
2 Likes

I achieved this (allow concurrency for master but preventing it for PRs) with a trick, by abusing the || operator:

concurrency:
  # group by workflow and ref; the last slightly strange component ensures that for pull
  # requests, we limit to 1 concurrent job, but for the master branch we don't
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.ref != 'refs/heads/master' || github.run_number }}
  # Cancel intermediate builds, but only if it is a pull request build.
  cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}

Of course if you use main instead of master, you need to adjust this. Alternatively, you can simply allow concurrency on all branches:

concurrency:
  # group by workflow and ref; the last slightly strange component ensures that for pull
  # requests, we limit to 1 concurrent job, but for the master branch we don't
  group: ${{ github.workflow }}-${{ github.ref }}-${{ startsWith(github.ref, 'refs/pull/') || github.run_number }}
  # Cancel intermediate builds, but only if it is a pull request build.
  cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
1 Like