【Tool】Pipeline architecture(Pipeline 架构)


Pipeline 是 GitLab CI/CD 中的基础构建块(building blocks)。本文记录了与其相关的几个重要概念。
你有三种常见方法来设计(structure)你的 pipelines,它们各有优势。如有需要,这些方法也可以混用或连用(mixed and matched):

  • Basic:适用于直观、简单的(straightforward)项目,所有的配置项都在一处容易找到的地方。
  • Directed Acyclic Graph:有向无环图 pipeline 适用于对执行效率有需求的大型复杂项目。
  • Child/Parent Pipelines:父子 pipeline 适用于带有大量自定义组件(independently defined components)的 monorepos 和项目。

对于下面提到的任何关键词,可以查询 CI YAML reference 来获取详情。

Basic Pipeline

这是 GitLab 中最简单的 pipeline,它先同时运行构建阶段(build stage)中的所有任务,当这些任务都完成的时候,再同时运行测试阶段中的所有任务,以此类推。这不是最高效的,如果你有许多步骤要做,它将变得相当复杂,但是它很容易维护。

deploy stage
test stage
build stage
deploy_a
deploy
deploy_b
test_a
test
test_b
build_a
build
build_b

与上图匹配的基本 ./gitlab-ci.yml pipeline 配置示例:

stages:
  - build
  - test
  - deploy

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

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

Directed Acyclic Graph(有向无环图)Pipelines

如果效率对你来说很重要,你希望所有的事情能尽快完成,你可以使用有向无环图(Directed Acyclic Graph) pipeline。使用关键词 needs 来定义任务之间的依赖关系。当 GitLab 清楚任务之间的关系时,它才能尽快执行需要执行的事情、甚至可能地话,直接跳进后面的阶段(stages)。
下面的示例中,如果 build_atest_abuild_btest_b 快很多,即使 build_b 仍未完成,GitLab 也可以直接开始 deploy_a

Pipeline using DAG
deploy_a
test_a
build_a
deploy_b
test_b
build_b

与上图匹配的 DAG /.gitlab-ci.yml 配置示例

stages:
  - build
  - test
  - deploy

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

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

Child / Parent Pipeline

上述示例中,很清晰的是我们有两个不同的事情可以独立构建。这也是通过使用关键词 trigger 使用父子(Child / Parent) pipeline 的理想案例。它把配置文件分成了多个文件,可令每个文件保持简洁。你可以通过以下方法进行组合:

  • 关键词 rules:例如,让子 pipeline 仅在某些情况下被触发。
  • 关键词 include:提供通用行为来避免重复编写配置文件。
  • 在子 pipeline 中使用 DAG pipelines 可以利用两种 pipeline 的优势。
Parent pipeline
child pipeline B
child pipeline A
trigger_b
trigger_a
deploy_a
test_a
build_a
deploy_b
test_b
build_b

与上图匹配的 /.gitlab-ci.yml 父 pipeline 配置文件

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/*

子 pipeline a 的配置文件,位于 /a/.gitlab-ci.yml,使用了 DAG 关键词 needs

stages:
  - build
  - test
  - deploy

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

子 pipeline b 的配置文件,位于 /b/.gitlab-ci.yml,使用了 DAG 关键词 needs

stages:
  - build
  - test
  - deploy

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

如果你在最后有些相同的配置步骤或者统一的部署操作,也可以设置一些任务在这些触发的子 pipeline 之前或者之后进行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jenkins pipeline 架构是一种基于脚本的流水线构建工具,它可以实现持续集成、持续交付和持续部署。Jenkins pipeline 架构由以下几个组件组成: 1. Jenkins Master:Jenkins pipeline 架构的核心组件,负责管理所有的节点和流水线。 2. Jenkins Agent:Jenkins pipeline 架构的工作节点,可以在不同的操作系统和环境中运行,执行流水线中的不同阶段。 3. Pipeline Script:Jenkins pipeline 架构的脚本文件,定义了流水线的整个过程和各个阶段的执行步骤。 4. Pipeline Plugin:Jenkins pipeline 架构的插件,提供了丰富的功能和工具,可以实现自动化测试、代码扫描、构建、部署等操作。 Jenkins pipeline 架构的流程包括以下几个步骤: 1. 定义 Pipeline Script:在 Jenkins Master 中创建 Pipeline Script,定义流水线的整个过程和各个阶段的执行步骤。 2. 使用 Pipeline Plugin:在 Pipeline Script 中使用 Pipeline Plugin,实现自动化测试、代码扫描、构建、部署等操作。 3. 执行 Pipeline:在 Jenkins Master 中执行 Pipeline Script,将流水线分为不同的阶段,并在不同的 Jenkins Agent 节点上执行。 4. 监控 Pipeline:实时监控 Pipeline 的执行情况,包括每个阶段的执行结果和日志输出。 5. 自动化部署:将最终的代码部署到生产环境中,并进行必要的测试和验证。 Jenkins pipeline 架构可以帮助开发团队实现持续集成、持续交付和持续部署,提高软件开发的效率和质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值