父子流水线 #

一、流水线类型 #

text
┌─────────────────────────────────────────────────────────────┐
│                    流水线类型                                │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  父子流水线                  多项目流水线                    │
│  ┌─────────────────┐        ┌─────────────────┐            │
│  │ • 同一项目      │        │ • 跨项目        │            │
│  │ • 模块化配置    │        │ • 项目间依赖    │            │
│  │ • 动态生成      │        │ • 触发下游      │            │
│  │ • 触发子流水线  │        │ • 传递变量      │            │
│  └─────────────────┘        └─────────────────┘            │
│                                                              │
└─────────────────────────────────────────────────────────────┘

二、父子流水线 #

基本概念 #

父流水线触发子流水线,子流水线独立执行。

yaml
stages:
  - build
  - test

build:
  stage: build
  script:
    - npm run build

child_pipeline:
  stage: test
  trigger:
    include: child-pipeline.yml

子流水线配置 #

yaml
stages:
  - test

unit_test:
  stage: test
  script:
    - npm run test:unit

integration_test:
  stage: test
  script:
    - npm run test:integration

等待子流水线完成 #

yaml
child_pipeline:
  stage: test
  trigger:
    include: child-pipeline.yml
    strategy: depend

多个子流水线 #

yaml
stages:
  - build
  - test

build:
  stage: build
  script:
    - npm run build

test_unit:
  stage: test
  trigger:
    include: test-unit.yml
    strategy: depend

test_integration:
  stage: test
  trigger:
    include: test-integration.yml
    strategy: depend

传递变量 #

yaml
child_pipeline:
  stage: test
  trigger:
    include: child-pipeline.yml
  variables:
    UPSTREAM_COMMIT: $CI_COMMIT_SHA
    DEPLOY_ENV: staging

子流水线访问变量:

yaml
job_name:
  script:
    - echo "Upstream commit: $UPSTREAM_COMMIT"
    - echo "Deploy env: $DEPLOY_ENV"

三、动态子流水线 #

动态生成配置 #

yaml
stages:
  - generate
  - test

generate-config:
  stage: generate
  script:
    - |
      cat > generated-config.yml << EOF
      stages:
        - test
      
      test:
        stage: test
        script:
          - echo "Dynamic test"
      EOF
  artifacts:
    paths:
      - generated-config.yml

child-pipeline:
  stage: test
  trigger:
    include:
      - artifact: generated-config.yml
        job: generate-config

基于目录生成 #

yaml
generate-config:
  stage: generate
  script:
    - |
      cat > generated-config.yml << EOF
      stages:
        - test
      EOF
      
      for dir in services/*/; do
        service=$(basename $dir)
        echo "
      test_${service}:
        stage: test
        script:
          - cd ${dir}
          - npm test
      " >> generated-config.yml
      done
  artifacts:
    paths:
      - generated-config.yml

child-pipeline:
  stage: test
  trigger:
    include:
      - artifact: generated-config.yml
        job: generate-config

矩阵触发 #

yaml
stages:
  - test

test:
  stage: test
  trigger:
    include: test.yml
  parallel:
    matrix:
      - SERVICE: [api, web, worker]
        ENV: [staging, production]

test.yml:

yaml
test:
  script:
    - echo "Testing $SERVICE in $ENV"

四、多项目流水线 #

基本用法 #

yaml
stages:
  - build
  - trigger

build:
  stage: build
  script:
    - npm run build

trigger_downstream:
  stage: trigger
  trigger:
    project: my-group/my-project
    branch: main

等待下游完成 #

yaml
trigger_downstream:
  stage: trigger
  trigger:
    project: my-group/my-project
    branch: main
    strategy: depend

传递变量 #

yaml
trigger_downstream:
  stage: trigger
  trigger:
    project: my-group/my-project
    branch: main
  variables:
    UPSTREAM_COMMIT: $CI_COMMIT_SHA
    DEPLOY_ENV: production

使用变量指定项目 #

yaml
variables:
  DOWNSTREAM_PROJECT: "my-group/my-project"

trigger_downstream:
  trigger:
    project: $DOWNSTREAM_PROJECT

五、流水线触发器 #

trigger关键字 #

yaml
trigger_job:
  trigger:
    project: my-group/my-project
    branch: main
    strategy: depend

trigger配置项 #

配置项 说明
project 目标项目
branch 目标分支
strategy 等待策略
include 包含的配置文件
forward 转发变量

forward配置 #

yaml
trigger_job:
  trigger:
    project: my-group/my-project
    forward:
      yaml_variables: true
      pipeline_variables: true

六、使用场景 #

1. 微服务项目 #

yaml
stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - npm run build

test_api:
  stage: test
  trigger:
    include: services/api/.gitlab-ci.yml
    strategy: depend

test_web:
  stage: test
  trigger:
    include: services/web/.gitlab-ci.yml
    strategy: depend

deploy:
  stage: deploy
  script:
    - echo "Deploying all services"
  needs:
    - test_api
    - test_web

2. 跨项目部署 #

yaml
stages:
  - build
  - deploy

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/

deploy_staging:
  stage: deploy
  trigger:
    project: my-group/infrastructure
    branch: main
    strategy: depend
  variables:
    ENVIRONMENT: staging
    APP_VERSION: $CI_COMMIT_SHA

deploy_production:
  stage: deploy
  trigger:
    project: my-group/infrastructure
    branch: main
    strategy: depend
  variables:
    ENVIRONMENT: production
    APP_VERSION: $CI_COMMIT_SHA
  only:
    - main
  when: manual

3. 多环境测试 #

yaml
stages:
  - test

test_chrome:
  stage: test
  trigger:
    include: e2e-test.yml
  variables:
    BROWSER: chrome

test_firefox:
  stage: test
  trigger:
    include: e2e-test.yml
  variables:
    BROWSER: firefox

test_safari:
  stage: test
  trigger:
    include: e2e-test.yml
  variables:
    BROWSER: safari

七、最佳实践 #

1. 模块化设计 #

text
project/
├── .gitlab-ci.yml (主配置)
├── services/
│   ├── api/
│   │   └── .gitlab-ci.yml
│   └── web/
│       └── .gitlab-ci.yml
└── tests/
    ├── unit.yml
    └── integration.yml

2. 使用strategy: depend #

yaml
child_pipeline:
  trigger:
    include: child.yml
    strategy: depend

3. 合理传递变量 #

yaml
trigger_job:
  trigger:
    project: my-group/my-project
  variables:
    UPSTREAM_COMMIT: $CI_COMMIT_SHA
    DEPLOY_ENV: $DEPLOY_ENV

4. 使用artifacts传递 #

yaml
build:
  artifacts:
    paths:
      - dist/

child_pipeline:
  trigger:
    include: child.yml

子流水线可以访问父流水线的artifacts。

5. 错误处理 #

yaml
child_pipeline:
  trigger:
    include: child.yml
    strategy: depend
  allow_failure: false

八、完整示例 #

微服务项目 #

主配置:

yaml
stages:
  - prepare
  - build
  - test
  - deploy

variables:
  REGISTRY: $CI_REGISTRY

prepare:
  stage: prepare
  script:
    - echo "Preparing..."
  artifacts:
    paths:
      - .env

build_api:
  stage: build
  trigger:
    include: services/api/.gitlab-ci.yml
    strategy: depend
  variables:
    SERVICE_NAME: api

build_web:
  stage: build
  trigger:
    include: services/web/.gitlab-ci.yml
    strategy: depend
  variables:
    SERVICE_NAME: web

test_integration:
  stage: test
  trigger:
    include: tests/integration.yml
    strategy: depend

deploy_staging:
  stage: deploy
  trigger:
    project: my-group/infrastructure
    branch: main
  variables:
    ENVIRONMENT: staging
  only:
    - develop

deploy_production:
  stage: deploy
  trigger:
    project: my-group/infrastructure
    branch: main
  variables:
    ENVIRONMENT: production
  only:
    - main
  when: manual

服务配置(services/api/.gitlab-ci.yml):

yaml
stages:
  - build
  - test

build:
  stage: build
  image: node:18
  script:
    - cd services/$SERVICE_NAME
    - npm ci
    - npm run build
  artifacts:
    paths:
      - services/$SERVICE_NAME/dist/

test:
  stage: test
  image: node:18
  script:
    - cd services/$SERVICE_NAME
    - npm test

下一步 #

现在你已经掌握了父子流水线,接下来让我们学习 性能优化

最后更新:2026-03-28