Merge Request - execute stage on each commit

When creating merge requests I have a number of stages such as checking formating, linting, compiling etc.

E.g.

formatting:
  stage: formatting
  image: rust
  before_script:
    - rustup component add rustfmt
  script:
    - cargo fmt --all -- --check
  rules:
    - if: $CI_MERGE_REQUEST_ID

My preference for merging is fast forwarding/rebasing. So I would like to run each stage upon ever commit, to ensure issues are fixed in the commit they are introduced, rather than just checking the HEAD of the merge request.

I tried removing

  rules:
    - if: $CI_MERGE_REQUEST_ID

and within the pipelines section of the merge request I can see it being ran for each commit. However, if one fails I am still able to merge the request as the pipeline for the merge request passed.

How am I able to make a merge request unmergeable if a pipeline failed for a commit within a branch I am attempting to merge?

I am not sure I understand your problem, but you can go to settings -> general -> Merge requests -> Merge checks and check the box Pipelines must succeed .

So yes that setting will enforce the pipeline for the merge request to pass i.e. stages which include the rule below before it can be merged.

  rules:
    - if: $CI_MERGE_REQUEST_ID

But I have stages I want to pass for every commit within the branch before the merge request can be merged.

E.g. below is from a merge request I have open. I have two stages that run per merge request, both pass so the UI will let me merge it.

Yet I also have stages I run for every commit within the branch and if any of them fail I do not want the merge request to be mergable. So you will see I had test failures on the commits pipeline but because the merge request pipeline was fine it would let me merge.

Screenshot_2021-10-02_15-36-12

Why you don’t include all the stages in the MR ?

I still don’t understand the problem here! Can you please provide an example .gitlab-ci.yml.

Why you don’t include all the stages in the MR ?

Because a merge request only tests the HEAD of the branch. I want each commit in the branch to be tested, because I am fast forward merging every commit onto the main branch and I want each commit to be atomic and revertable.

Here is the link to the merge requests .gitlab-ci.yml not sure how it will help.

Here is what we do in the projects we work on.

On the first push, in a feature branch, we create an MR. and on the next pushes to the MR, the entire pipeline is run against the commit. And if something is wrong we are not allowed to merge the MR.

When we are ready we rebase and ff-merge the MR.

Does this answer your question or still don’t get your point ?

This kind of works but it is a work around with plenty of edge cases.

E.g.

  • You push multiple commits at once and only for the latests commit will the pipeline be ran.
  • If a commit has a failing pipeline and you push another commit which the pipeline passes for you can still fast forward merge.
  • If you rebase, squash or change any prior commits then the pipeline will not be ran for them etc.
  • You push multiple commits at once and only for the latests commit will the pipeline be ran.
  • If a commit has a failing pipeline and you push another commit which the pipeline passes for you can still fast forward merge.
  • If you rebase, squash or change any prior commits then the pipeline will not be ran for them etc.

I don’t think you can avoid these drawbacks in gitlab CI unless you push each time you commit in your local machine.

Well that would just solve the first edge cases I listed not the others.

Anyways, I am looking for a more officially supported method of including the pipeline results for each commit in a branch’s merge request if anybody can help?