-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fixed #12325 - cancel workflows if a newer commit is pushed on the same branch #7636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Thanks for your contribution. I looked into adding concurrency for our jobs before but it has a major shortcoming that you cannot limit it to branches. That will lead to jobs on Also see https://trac.cppcheck.net/ticket/12325. I am curious though why the workflow is being triggered for random branches in your case since we limited the branches this is triggered on: on:
push:
branches:
- 'main'
- 'releases/**'
- '2.*'
tags:
- '2.*'
pull_request: |
Thanks for your prompt reply. I think what you describe can be achieved by the following syntax (given that we insert it in all .yml files), but I will test it just to be sure and then get back to you: concurrency:
group: ${{ github.event_name }}-${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }} # this will cancel only PRs Would you mind explaining why you do not want this:
Thanks a lot! |
That is so obvious and simple I am flabbergasted why this couldn't be done back then. I will see if I can find my old WIP code tomorrow and see if that gives me a hint why.
We want the CI "all green" on code which will be used. If we would cancel builds this might not be the case if a lot of stuff is merged and would make it harder to determine which commit actually caused the issue. Also most of the time in the CI is currently "wasted" by our code because of performance issues. There are WIP PRs to improve things. |
Thanks for working on this. I envision that this could mean that fewer pipelines will be queued and that you will get the CI result a little faster for your latest push. That is good!
I also agree with @firewave. |
This is interesting and makes sense.
Exactly! |
I found my old changes and I think the problem I was facing was finding a solution to generate groups which work with pushes and PRs which you did solve with I was not aware that strings could be used in concatenation code - looking at it I would have assume that the expression would resolve to And I also did not think about just leveraging Regrading some question I had about this back then - can you confirm the the groups are just enforced across repos/forks and not globally? Otherwise the group needs to add I will also do some tests later on to see if it behaves as expected. |
I confirm; workflows in this repo's I have just pushed the improved version, let me know if I can do anything else. For example, I could add it to all workflows that trigger on PR if you wish. |
That would be great. But let me run some tests first. Will do later today. |
Another point is that we do not require PRs to be at HEAD to be merged. So even though no merge conflicts are shown the changes from various PRs might not be compatible with each other. That's why we also need to do the workflows for the Also a note on why we filter the workflow. We had the problem that when you create a PR from a branch in the same repo you want to merge it into the builds would run twice. Once for the PR and once for the push to the branch. This is making it harder to test changes before you open a PR even if you are not working on the repo the PR will merge into. |
Oh, I see, that makes sense!
Right, we were also thinking about this problem and how to propose an optimization to overcome it, but we couldn't come up with something that can be applied automatically to an arbitrary yaml file. What you have done here seems to work great. |
Sorry for not testing the changes yet, but something else came up.
Except for the shortcoming. And I am not too happy about it since i affects all forks. But so far "it works". An optimizations we also implemented is using |
Okay, I gave it a spin. This prevents the workflows from running more than once on But that really doesn't matter because in our case the concurrency should not be applied when in the non-PR case at all (I have the feeling that this is what I was actually hitting on back then). I wonder if an empty |
We can make this work this by making the concurrency a unique ID for the non-PR case: concurrency:
group: ${{ github.event_name }}-${{ github.workflow }}-${{ github.head_ref || github.sha }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }} # this will cancel only PRs |
In that case we might not even need the conditional value for |
Oh no - that is all wrong (it's too warm). That is not set for PRs. It should be: concurrency:
group: ${{ github.event_name }}-${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }} # this will cancel only PRs |
This will have the same effect with the changes in this PR, under the condition that from a single branch, only one PR can be opened. |
echo "${{ github.event_name }}-${{ github.workflow }}-${{ github.head_ref }}"
echo "${{ github.event_name }}-${{ github.workflow }}-${{ github.ref }}"
echo "${{ github.event_name }}-${{ github.workflow }}-${{ github.sha }}"
echo "${{ github.event_name }}-${{ github.workflow }}-${{ github.event.pull_request.number }}" main (push)
branch (push)
branch (pull_request)
I cannot think right now - it is just too warm ... |
:D Thanks for the echo results. I will also have another look and try to prepare a couple of illustrative examples. |
In the meantime, you may also have a look at this one: #7641 The change is much simpler, we would be curious to see what you think of the idea. |
I had another look in this, and I think your suggestion is better. It also works in the case where from branch In your poposal, the Given that, I could apply your proposal if you wish. |
Will have another look later today.
I think this is impossible in GitHub and there is a 1-to-1 relation between branches and PRs. A look at https://github.com/danmar/cppcheck/branches seems to support that. |
That is very interesting. That should not affect our main repo but might forks. Bot something I think we should consider for this repo. |
Signed-off-by: Konstantinos <[email protected]>
Signed-off-by: Konstantinos <[email protected]>
|
Change Summary
Currently, if a push to
branchA
triggers the CodeQL workflow, and shortly after a subsequent push on the samebranchA
triggers the CodeQL workflow, both runs will run. With these changes, the first run will be cancelled, thus saving compute resources (see below for quantity) without sacrificing functionality, since the second run will contain the changes from the first push as well.The introduced syntax comes from the official documentation.
Context
Hi,
We are a team of researchers from University of Zurich and we are currently working on energy optimizations in Github Actions workflows.
Based on our offline analysis, cancelling CodeQL runs triggered by a now-obsolete commit (because a new commit has triggered the CodeQL workflow again), could have saved ~49.9 CPU hours since the beginning of the project.
For example, the commit 70b1f triggered this workflow run, and one minute later the commit ea2c3, that happened on top of the first commit, triggered this workflow. Both workflows ran till the end, spending ~12 CPU minutes each. With the proposed changes, the first run would be cancelled, hence saving ~11 CPU minutes and clearing the queue for other workflows.
Kindly let us know (here or in the email below) if you would like more details on our offline evaluation, if you want to reject the proposed changes for other reasons, or if you have any question whatsoever.
Best regards,
Konstantinos Kitsios
[email protected]