Detached jobs for merge-request

Hello there,

We do have a pipeline with 3 stages: Verify → Build → Deploy.

  • Verify should be run for all commits (and merge-requests)
  • Build is automatic for the main branch and manual for others
  • Deploy is automatic for the main branch and needs the Build stage

Our issue is that we have pipelines now, the expected one and a “detached” one.

What does that means, can we get rid of that ?

Here is the relevant part of our .gitlab-ci.yml file:

verify:webapi:
  stage: verify
  ...
  rules:
    - changes:
        - src/**/*
      when: on_success

build:webapi_docker_file:
  stage: build
  needs:
    - verify:webapi
  ...
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - if: $CI_COMMIT_BRANCH == $MAIN_BRANCH
      changes:
        - src/**/*
      when: on_success

deploy:webapi_docker_image:
  stage: deploy
  needs:
    - build:webapi_docker_file
  ...
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - if: $CI_COMMIT_BRANCH == $MAIN_BRANCH
      changes:
        - src/**/*
      when: on_success
      
build:webapi_docker_feature_file:
  stage: build
  needs:
    - verify:webapi
  ...
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH != $MAIN_BRANCH
      changes:   
        - src/**/*
      when: manual

deploy:webapi_docker_feature_image:
  stage: deploy
  needs:
    - build:webapi_docker_feature_file
  ...
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH != $MAIN_BRANCH
      changes:
        - src/**/*
      when: on_success

deploy:webapi_on_dev:
  stage: deploy
  needs:
   # That's normal. We deploy the "sources", docker image is for another use
    - verify:webapi
  ...
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - if: $CI_COMMIT_BRANCH == $MAIN_BRANCH
      changes:
        - src/**/*
      when: on_success
  environment:
    name: development
    ...

It seems you are inadvertently creating unspecified merge request pipelines that show as detached because some jobs contain:

  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never

but no jobs have the below to specify which jobs should run on merge requests

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

I am not sure if I entirely understand which jobs you are intending to run on merge requests in a merge request pipeline, but I hope this helps you diagnose your issue.

I recommend looking into workflows as well, these allow you to create and differentiate between merge request pipelines and branch pipelines in the UI. This also ensures you have access to these merge request pipelines specific predefined variables.