4

In Gitlab I am trying to trigger child pipeline from parent pipeline. The child pipeline is within the same project under subdirectory. However, while triggering at the time of merge request event it is giving error "downstream pipeline cannot be created, No stages/jobs for this pipeline"

Folder structure:

  • Namespace/Project/.gitlab-ci.yml (parent pipeline)
  • Namespace/Project/servicename/.gitlab-ci.yml (child pipeline)

Parent pipeline:

trigger_servicename:
 stage: triggers  
  rules:        
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "dev"'
      changes: 
        - app-notifier/*      
      when: always  
  trigger:
    include: servicename/.gitlab-ci.yml    
    strategy: depend     

Child pipeline:

image:

    name: registry.gitlab.com/who-docker/aws-cli:latest

    entrypoint: 
      - /usr/bin/env
      - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
stages:  
  -build
build:
  stage: build                 
  script:    
    - echo "run build..."
        
test:
  stage: test                 
  script:             
    - echo "run test...."
sigur
  • 662
  • 6
  • 21
mair
  • 325
  • 1
  • 5
  • 14

3 Answers3

27

In general you will get the error message "downstream pipeline cannot be created, No stages/jobs for this pipeline" when there is no rule that matches any of the jobs in the child pipeline. Rules from upstream pipelines will be inherited in child pipelines.

Looking at your example the rule if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "dev"' is inherited to the child pipeline. This rule does not match in the child pipeline, because $CI_PIPELINE_SOURCE in a child pipeline is trigger, and not the one from the upstream pipeline. Consequently, no jobs are available for gitlab to execute.

If you add

workflow:
  rules:
    - when: always

to your child pipeline it will work. Modify the rules accordingly if required.

Michael Osl
  • 2,720
  • 1
  • 21
  • 36
  • Thanks! Understanding that rules are inherited by child pipelines is solving the actual problem. I did not find this stated anywhere in the documentation. – chronicc Apr 16 '22 at 08:49
  • 3
    That's actually ridiculous, why would it automatically inherit a rule when you are deliberately including it to be executed. Like you literally can set a rule on the triggerer job that says when the child jobs should be executed, no need for any further rules – Beefcake Oct 26 '22 at 16:24
2

A child pipeline is not triggered automatically. The jobs in the child YAMl file are matched agains the rules section of the jobs. To add a job to the task list, add a rule to the workflow.

For example:

Parent gitlab configuration

trigger_servicename:
   trigger:
       include: servicename/.gitlab-ci.yml    
       strategy: depend 

Child gitlab configuration

child_service:
    image: my-image-name
    rules:
        - if: '$CI_PIPELINE_SOURCE == "parent_pipeline"'
  • The rule: '$CI_PIPELINE_SOURCE == "pipeline"' Runs a job in the (parent) flow.
  • The rule: '$CI_PIPELINE_SOURCE == "parent_pipeline"' runs a job in the downstream of a parent pipeline.
Kasper
  • 31
  • 2
0

For a monorepo with multiple projects I actually did something like:

nameofjob:
  stage: trigger
  trigger:
    include:
      - artifact: folder/.gitlab-ci.yml
    strategy: depend

For an external project -

include:
  - project: 'my-group/my-pipeline-library'
    ref: 'main'
    file: '/path/to/child-pipeline.yml'

https://docs.gitlab.com/ee/ci/pipelines/parent_child_pipelines.html

Mark
  • 2,429
  • 20
  • 20