{{< details >}}

  • Tier: 基础版, 专业版, 旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

极狐GitLab 的流水线是 CI/CD 的基本构建块。本页面记录了一些与它们相关的重要概念。

你可以通过不同的方法来构建流水线,每种方法都有其自身的优势。这些方法可以根据需要进行混合和匹配:

  1. 基础流水线:适用于所有配置都集中在一个地方的简单项目。
  2. 使用 needs 关键字的流水线:适用于需要高效执行的大型复杂项目。
  3. 父子流水线:适用于 monorepos 和包含大量独立定义组件的项目。
  4. 多项目流水线:适用于需要跨项目相互依赖的大型产品,如具有微服务架构的产品。

例如,你可以从三个不同的极狐GitLab 项目中部署你的 Web 应用程序。通过多项目流水线,你可以在每个项目中触发一个流水线,每个项目都有自己的构建、测试和部署过程。你可以在一个地方可视化连接的流水线,包括所有跨项目的相互依赖关系。

基础流水线

基础流水线是极狐GitLab 中最简单的流水线。它在构建阶段并行运行所有任务,一旦所有任务完成,它以同样的方式运行测试和后续阶段的所有任务。这不是最有效的,如果你有很多步骤,它可能会变得相当复杂,但它更容易维护:

%%{init: { "fontFamily": "GitLab Sans" }}%% graph LR accTitle: Basic pipelines accDescr: Shows a pipeline that runs sequentially through the build, test, and deploy stages. subgraph deploy stage deploy --> deploy_a deploy --> deploy_b end subgraph test stage test --> test_a test --> test_b end subgraph build stage build --> build_a build --> build_b end build_a -.-> test build_b -.-> test test_a -.-> deploy test_b -.-> deploy

匹配图示的基础 /.gitlab-ci.yml 流水线配置示例:

stages:
  - build
  - test
  - deploy

default:
  image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something."

build_b:
  stage: build
  script:
    - echo "This job builds something else."

test_a:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."

test_b:
  stage: test
  script:
    - echo "This job tests something else. It will only run when all jobs in the"
    - echo "build stage are complete too. It will start at about the same time as test_a."

deploy_a:
  stage: deploy
  script:
    - echo "This job deploys something. It will only run when all jobs in the"
    - echo "test stage complete."
  environment: production

deploy_b:
  stage: deploy
  script:
    - echo "This job deploys something else. It will only run when all jobs in the"
    - echo "test stage complete. It will start at about the same time as deploy_a."
  environment: production

使用 needs 关键词的流水线

如果效率很重要,并且你希望一切尽可能快地运行,你可以使用 needs 关键词 来定义作业之间的依赖关系。当极狐GitLab 知道作业之间的依赖关系时,作业可以尽可能快地运行,甚至可以在同一阶段的其他作业之前开始。

在下面的示例中,如果 build_atest_abuild_btest_b 快得多,即使 build_b 仍在运行,极狐GitLab 也会启动 deploy_a

%%{init: { "fontFamily": "GitLab Sans" }}%% graph LR accTitle: Pipeline using needs accDescr: Shows how two jobs can start without waiting for earlier stages to complete subgraph Pipeline using needs build_a --> test_a --> deploy_a build_b --> test_b --> deploy_b end

匹配图示的 /.gitlab-ci.yml 配置示例:

stages:
  - build
  - test
  - deploy

default:
  image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something quickly."

build_b:
  stage: build
  script:
    - echo "This job builds something else slowly."

test_a:
  stage: test
  needs: [build_a]
  script:
    - echo "This test job will start as soon as build_a finishes."
    - echo "It will not wait for build_b, or other jobs in the build stage, to finish."

test_b:
  stage: test
  needs: [build_b]
  script:
    - echo "This test job will start as soon as build_b finishes."
    - echo "It will not wait for other jobs in the build stage to finish."

deploy_a:
  stage: deploy
  needs: [test_a]
  script:
    - echo "Since build_a and test_a run quickly, this deploy job can run much earlier."
    - echo "It does not need to wait for build_b or test_b."
  environment: production

deploy_b:
  stage: deploy
  needs: [test_b]
  script:
    - echo "Since build_b and test_b run slowly, this deploy job will run much later."
  environment: production

父子流水线

随着流水线的日益复杂,会出现一些相关的问题:

  1. 阶段结构中,下一阶段的第一个作业必须在当前阶段所有步骤完成后才能开始,这导致了等待,从而减慢速度。
  2. 单一全局流水线的配置变得难以管理。
  3. 使用 include 的导入增加了配置的复杂性,并可能导致命名空间冲突,导致作业无意中重复。
  4. 流水线 UX 需要处理的作业和阶段太多。

此外,有时流水线的行为需要更加动态。选择启动子流水线(或不启动)的能力是一种强大的能力,特别是当 YAML 是动态生成时。

在上述基础流水线needs 流水线示例中,有两个软件包可以独立构建。这些情况非常适合使用父子流水线。它将配置分离到多个文件中,使其更简单。你可以将父子流水线与以下内容结合使用:

  1. rules 关键词:例如,仅在某个区域有更改时触发子流水线。
  2. include 关键词:引入常见行为,确保不重复自己。
  3. 子流水线内的needs 关键词,实现两者的优势。
%%{init: { "fontFamily": "GitLab Sans" }}%% graph LR accTitle: Parent and child pipelines accDescr: Shows that a parent pipeline can trigger independent child pipelines subgraph Parent pipeline trigger_a -.-> build_a trigger_b -.-> build_b subgraph child pipeline B build_b --> test_b --> deploy_b end subgraph child pipeline A build_a --> test_a --> deploy_a end end

匹配图示的父流水线 /.gitlab-ci.yml 配置示例:

stages:
  - triggers

trigger_a:
  stage: triggers
  trigger:
    include: a/.gitlab-ci.yml
  rules:
    - changes:
        - a/*

trigger_b:
  stage: triggers
  trigger:
    include: b/.gitlab-ci.yml
  rules:
    - changes:
        - b/*

a 流水线配置示例,位于 /a/.gitlab-ci.yml,使用 needs 关键词:

stages:
  - build
  - test
  - deploy

default:
  image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something."

test_a:
  stage: test
  needs: [build_a]
  script:
    - echo "This job tests something."

deploy_a:
  stage: deploy
  needs: [test_a]
  script:
    - echo "This job deploys something."
  environment: production

b 流水线配置示例,位于 /b/.gitlab-ci.yml,使用 needs 关键词:

stages:
  - build
  - test
  - deploy

default:
  image: alpine

build_b:
  stage: build
  script:
    - echo "This job builds something else."

test_b:
  stage: test
  needs: [build_b]
  script:
    - echo "This job tests something else."

deploy_b:
  stage: deploy
  needs: [test_b]
  script:
    - echo "This job deploys something else."
  environment: production

在极狐GitLab 中,可以设置作业在触发子流水线之前或之后运行,允许进行常见的设置步骤或统一的部署。