GitLab parent-child pipelines with rules:changes they trigger even if nothing change for MR pipelines but works for branch pipelines

I’m using the t Parent child pipelines example as a base ,and it works. It runs the child pipeline “a” only when the “a/*” changes, etc.

I want to run this pipeline only for merge request so I added the following at the top parent .gitlab-ci.yml

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

When I do this then suddenly all the child pipelines are executed even if the commit only changes a file in a/dddd.

Pasted_Image_2023-10-18__14_49

if I change to only run for branch pipelines with

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH

then it will do the right thing and only execute the child pipeline “trigger_a”

Using gitlab 16.4.1 (self managed)

I guess the problem is related to the downstream pipelines having another CI_PIPELINE_SOURCE (pipeline, parent_pipeline),

But really is there any way to make the MR pipeline behave exactly as the Branch pipeline in the regards of child pipelines ?

Ok, my mistake there are several things wrong

First, both child pipelines run because the MR request “change set” includes all the files changed between the source branch and the destination branch, not just the changes in the latest push to the branch. So in my case that includes changes in a/* and b/* that’s why both child pipelines run .

But in general after finding Run child pipelines with merge request pipelines I found that the best setup is the following;

.gitlab-ci.yml

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

a/.gitlab-ci.yml

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"

b/.gitlab-ci.yml

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "parent_pipeline"

This allows MR pipelines and branch pipelines, avoiding running the branch pipeline if there already a MR for that branch (and thus running only a MR pipeline in that case).

The workflow:rules in the child pipelines t prevents the downstream pipeline can not be created. Pipeline will not run for the selected trigger.The rules configuration prevented any jobs from being added to the pipeline error by configuring the child pipeline jobs to run when triggered by the parent pipeline on a MR pipeline (this is not necessary if running the pipelines as branch pipelines only, but as soon as you want to have MR pipelines, you need to add it)

1 Like