21

We have a normal repository, with some code and tests.

One job has 'rules' statement:

  rules:
    - changes:
      - foo/**/*
      - foo_scenarios/**/*
      - .gitlab-ci.yml

The problem is that presence of rules causes Gitlab to run 'detached pipeline', which wasn't my intention, and it's annoying. Is there any way to disable those 'detached' pipelines, but keep the rules section in place?

IKo
  • 4,998
  • 8
  • 34
  • 54
George Shuklin
  • 6,952
  • 10
  • 39
  • 80

3 Answers3

17
rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - changes:
      - foo/**/*
      - foo_scenarios/**/*
      - .gitlab-ci.yml
      when: always

I have not tested this, but I believe this is what you are looking for. This page and this one too are both easily navigable and are very helpful for finding the answer to basic gitlab-ci.yml questions.

Edit- Gitlab will evaluate the rules in order, and it stops evaluating subsequent rules as soon as one of the conditions are met. In this case, it will evaluate if: '$CI_PIPELINE_SOURCE == "merge_request_event"' first, and if it evaluates to true, no more rules will be checked. If the first rule evaluates to false, it will move on to the next rule.

Benjamin
  • 526
  • 6
  • 16
12

Things are actually more complicated as it depends from case to case. So this solution may work for you, but someone else may need to tweak it a bit.

Here is my understanding of it. As soon as you add rules: to your pipeline, you will override some defaults which prevent the merge request pipeline from begin created.

The solution suggested by @Benjamin works, but as you have noticed, needs to be added to every job. So a lot of repeated configuration for most jobs.

I would suggest looking into workflow: that allows you to define a default behavior. You will need rules only for the jobs that have special rules.

Here is an example:

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'

If you wish to go a bit deeper into this behavior, I have written an article about this (friend link for anyone without a Medium subscription):

Fix GitLab CI Duplicate Pipelines in Merge Requests when Using rules:

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
  • There is not necessarily a need to repeat configuration(depending on what you mean by that) as the rules can be added to a job, then extended to other job using the `extends` keyword or using yaml anchors. – Benjamin Dec 20 '21 at 22:13
  • @Benjamin can you provide an example of that? because adding extends to each job and adding the rule itself seems like - again - doing much more than adding a few global workflow lines in the head of the file – genuinefafa Mar 16 '22 at 13:17
  • I can't add a large code block to a comment, otherwise I would, and all of my pipeline configs are within my company's private Gitlab group so I cannot share them. The first answer in this question shows an example though https://stackoverflow.com/questions/53175030/gitlab-ci-extend-script-section You can create a hidden "rules" job `.rules-template:` and define rules you want to reuse and then in each job `extends: -.rules-template`. The other options would be to use yaml anchors with a hidden block(or use them with a set of rules you defined in another job) instead of extends. – Benjamin Mar 18 '22 at 12:20
0

Gitlab have an out-of-the-box solution to this, see https://docs.gitlab.com/ee/ci/yaml/workflow.html#workflowrules-templates

include:
  - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml'
Matthew
  • 10,361
  • 5
  • 42
  • 54