-1
 rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH

I've seen this gitlab rule fx here, what I don't really get is what the purpose if $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS is. As I understand it is when a commit has been made to a branch that have an open merge request to it. Why would one want to handle that case specially?

1
  • Not the downvoter, but the "Why?" question might attract opinions. If you read it as "What consequences (benefits or drawbacks) come from treating that case specially?" though, this isn't really a question for opinions. Commented Jul 29 at 14:22

2 Answers 2

1

If you enabled "merged results", then you don't want to run a pipeline on the newly pushed commit. Instead, you want a pipeline to run on the temporary commit that merges the tip of your branch with the target branch of the merge request.

In short, it avoids running pipelines who's results are not meaningful anyway.

You can achieve similar behaviour much better though:

  • Add all permanent branches (e.g. master/main, but also X.Y style release branches) to the list of protected branches.
  • $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_PROTECTED == "true" -- use this condition to create a pipeline on pushes to protected branches.
  • $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_EVENT_TYPE != "detached" -- use this to run a pipeline for merge requests. The != "detached" will exclude merge requests with merge conflicts. You can't merge them anyway and after you fixed the conflict, a pipeline is created, too.
Sign up to request clarification or add additional context in comments.

2 Comments

In what way(s) is the alternative approach better? In what way does it differ. I guess that one could assume that one protects the permanent branches anyway, it sounds like a good idea.
One thing is that it doesn't start a pipeline when you just push to and create a new branch. Only when you create the merge request it creates the pipeline. The other thing I don't like is how it puts the conditions into code. if: $CI_COMMIT_BRANCH -- what does that mean? In order to understand it, you need to study documentation that tells you when that var is defined and when not, probably for (some) merge request events and (some) push events. Spelling it out explicitly like "on X event under the condition Y" like I did seems clearer to me.
0

basically what is says is :

If the pipeline has been triggered from a merge request -> run the pipeline

If there is a merge request opened for this branch -> do not run

If there is no merge request opened -> run the pipeline

Basically what is says is run either for the main/dev branches, or run only if in a merge request.

2 Comments

Yeah, basically the question said that I got that. The question was why one would like to do that.
You may only want to run certain checks if they are going to end up in one of the main branches, so either in the main branch itself, on in the code that will be merged into it. For example if you were to rework all your codebase, you wouldn't want the ci from the previous project to run on your experiment branch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.