Concurrecy not work for push

Hi community,

When I tried to use the new feature concurrency in Github Actions, I find I don’t know how to choose the appropriate field for group. The example provided from documents is ${{ github.head_ref }} which only works for or. For actions triggered by both push and or, I find a common choice is ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} from MONAI and airflow, but unfortunately it is not working in my test: test: concurrency by Yiyiyimu · Pull Request #8 · Yiyiyimu/GithubActionsTest · GitHub

Is there any other recommendation on what to fill in group?

It depends exactly what level of concurrency you want. You could use github.workflow to have only one instance of the workflow running at a time, regardless of which branch triggered it, or even github.repository to only have one workflow running at a time inside the whole repository.

Can you give some more specifics on exactly what you’re aiming for when setting this up? That way we might be able to suggest a better option for you to use.

Hi Thomas thanks for your prompt reply and glad to know the supported function of concurrency!

Actually what I want is to limit it to branch level. For example, if one action would be triggered by both push and pull_request, I want only two workflows running for this branch/PR, one for push and one for pr, and the latest commit would cancel the workflows triggered by previous ones. How should I implement this?

The last setup you tried looks like it should work and do what you want.

name: test

on: [push, pull_request]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

I would expect a second push to the same branch to cancel an in-flight workflow run triggered by a push.

Do I understand correctly, that both workflows are executed in parallel and both succeed instead of one being canceled and the other succeeding?

Thank you Simran it solved the problem! It seems I forgot to make another push to test it :rofl:

1 Like

I’d like to do the same thing as @Yiyiyimu but I want the concurrency setting to apply to every branch EXCEPT the master branch. Is there a way to do this?

I guess something like this should work:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/master' && github.run_number || github.ref }}
  cancel-in-progress: true

If it’s a push to the master branch, then it uses the run number which is unique (except if you re-run a workflow), so it shouldn’t cancel other concurrent runs. If it’s not the master branch, then only a single run per branch is allowed.

You could probably use github.run_id or github.sha instead of github.run_number with the same result.

2 Likes

i hope your problem was solved