Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable 'detached' pipelines in Gitlab?

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?

like image 439
George Shuklin Avatar asked Aug 27 '21 14:08

George Shuklin


2 Answers

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.

like image 85
Benjamin Avatar answered Sep 20 '22 22:09

Benjamin


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:

like image 30
Valentin Despa Avatar answered Sep 21 '22 22:09

Valentin Despa