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?

3 Answers 3

16
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.

1
  • It worked, thank you. It need to be added for each job with rules. Aug 30, 2021 at 11:15
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:

3
  • 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, 2021 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 Mar 16, 2022 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 stackoverflow.com/questions/53175030/… 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, 2022 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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.