Gitlab CI 多项目流水线

Gitlab CI multi project pipeline

在 Gitlab 中,我试图从父管道触发子管道。子管道位于同一项目的子目录下。但是,在合并请求事件时触发时会出现错误“无法创建下游管道,此管道没有 stages/jobs

文件夹结构:

父管道:

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     

子管道:

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

对于具有多个项目的 monorepo,我实际上做了类似的事情:

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

对于外部项目 -

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

一般情况下,当没有匹配任何规则时,您会收到错误消息“无法创建下游管道,此管道没有 stages/jobs”子管道中的作业。来自上游管道的规则将在子管道中继承。

查看您的示例,规则 if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "dev"' 继承到子管道。此规则在子管道中不匹配,因为子管道中的 $CI_PIPELINE_SOURCEtrigger,而不是上游管道中的规则。因此,没有作业可供 gitlab 执行。

如果你添加

workflow:
  rules:
    - when: always

对于您的子管道,它将起作用。如果需要,相应地修改规则。