流水线架构

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

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

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

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

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

基础流水线#

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

Rendering chart...

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

yaml
1stages: 2 - build 3 - test 4 - deploy 5 6default: 7 image: alpine 8 9build_a: 10 stage: build 11 script: 12 - echo "This job builds something." 13 14build_b: 15 stage: build 16 script: 17 - echo "This job builds something else." 18 19test_a: 20 stage: test 21 script: 22 - echo "This job tests something. It will only run when all jobs in the" 23 - echo "build stage are complete." 24 25test_b: 26 stage: test 27 script: 28 - echo "This job tests something else. It will only run when all jobs in the" 29 - echo "build stage are complete too. It will start at about the same time as test_a." 30 31deploy_a: 32 stage: deploy 33 script: 34 - echo "This job deploys something. It will only run when all jobs in the" 35 - echo "test stage complete." 36 environment: production 37 38deploy_b: 39 stage: deploy 40 script: 41 - echo "This job deploys something else. It will only run when all jobs in the" 42 - echo "test stage complete. It will start at about the same time as deploy_a." 43 environment: production

使用 needs 关键词的流水线#

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

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

Rendering chart...

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

yaml
1stages: 2 - build 3 - test 4 - deploy 5 6default: 7 image: alpine 8 9build_a: 10 stage: build 11 script: 12 - echo "This job builds something quickly." 13 14build_b: 15 stage: build 16 script: 17 - echo "This job builds something else slowly." 18 19test_a: 20 stage: test 21 needs: [build_a] 22 script: 23 - echo "This test job will start as soon as build_a finishes." 24 - echo "It will not wait for build_b, or other jobs in the build stage, to finish." 25 26test_b: 27 stage: test 28 needs: [build_b] 29 script: 30 - echo "This test job will start as soon as build_b finishes." 31 - echo "It will not wait for other jobs in the build stage to finish." 32 33deploy_a: 34 stage: deploy 35 needs: [test_a] 36 script: 37 - echo "Since build_a and test_a run quickly, this deploy job can run much earlier." 38 - echo "It does not need to wait for build_b or test_b." 39 environment: production 40 41deploy_b: 42 stage: deploy 43 needs: [test_b] 44 script: 45 - echo "Since build_b and test_b run slowly, this deploy job will run much later." 46 environment: production

父子流水线#

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

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

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

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

  1. rules 关键词:例如,仅在某个区域有更改时触发子流水线。
  2. include 关键词:引入常见行为,确保不重复自己。
  3. 子流水线内的needs 关键词,实现两者的优势。
Rendering chart...

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

yaml
1stages: 2 - triggers 3 4trigger_a: 5 stage: triggers 6 trigger: 7 include: a/.gitlab-ci.yml 8 rules: 9 - changes: 10 - a/* 11 12trigger_b: 13 stage: triggers 14 trigger: 15 include: b/.gitlab-ci.yml 16 rules: 17 - changes: 18 - b/*

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

yaml
1stages: 2 - build 3 - test 4 - deploy 5 6default: 7 image: alpine 8 9build_a: 10 stage: build 11 script: 12 - echo "This job builds something." 13 14test_a: 15 stage: test 16 needs: [build_a] 17 script: 18 - echo "This job tests something." 19 20deploy_a: 21 stage: deploy 22 needs: [test_a] 23 script: 24 - echo "This job deploys something." 25 environment: production

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

yaml
1stages: 2 - build 3 - test 4 - deploy 5 6default: 7 image: alpine 8 9build_b: 10 stage: build 11 script: 12 - echo "This job builds something else." 13 14test_b: 15 stage: test 16 needs: [build_b] 17 script: 18 - echo "This job tests something else." 19 20deploy_b: 21 stage: deploy 22 needs: [test_b] 23 script: 24 - echo "This job deploys something else." 25 environment: production

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