257.md 6.5 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
# Pipeline Architecture

> 原文:[https://docs.gitlab.com/ee/ci/pipelines/pipeline_architectures.html](https://docs.gitlab.com/ee/ci/pipelines/pipeline_architectures.html)

*   [Basic Pipelines](#basic-pipelines)
*   [Directed Acyclic Graph Pipelines](#directed-acyclic-graph-pipelines)
*   [Child / Parent Pipelines](#child--parent-pipelines)

# Pipeline Architecture[](#pipeline-architecture "Permalink")

管道是 GitLab 中 CI / CD 的基本构建块. 本页记录了一些与它们有关的重要概念.

构造管道的主要方法有三种,每种都有自己的优势. 如果需要,可以混合使用以下方法:

*   [基本](#basic-pipelines) :适用于简单的项目,其中所有配置都在一个易于查找的位置.
*   [有向无环图](#directed-acyclic-graph-pipelines) :适用于需要有效执行的大型,复杂项目.
*   [子级/父级管道](#child--parent-pipelines) :适用于具有大量独立定义组件的 monorepos 和项目.

有关下面使用的任何关键字的更多详细信息,请查看我们的[CI YAML 参考](../yaml/README.html)以获取详细信息.

## Basic Pipelines[](#basic-pipelines "Permalink")

这是 GitLab 中最简单的管道. 它会在构建阶段同时运行所有内容,一旦所有这些操作完成,它将以相同的方式在测试阶段运行所有内容,依此类推. 它不是最有效的,并且如果您有很多步骤,它可能会变得非常复杂,但更易于维护:

图 LR 子图部署阶段部署-> deploy_a 部署-> deploy_b 结束子图测试阶段测试-> test_a 测试-> test_b 结束子图构建阶段构建-> build_a 构建-> build_b 结束 build_a -.->测试 build_b -.->测试 test_a -.->部署 test_b -.->部署

匹配该图的示例基本`/.gitlab-ci.yml`管道配置:

```
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-pipelines "Permalink")

如果效率对您很重要,并且您希望所有内容尽快运行,则可以使用有向无[环图(DAG)](../directed_acyclic_graph/index.html) . 使用[`needs`关键字](../yaml/README.html#needs)定义作业之间的依赖关系. 当 GitLab 知道您的工作之间的关系时,它可以尽快运行所有内容,甚至在可能的情况下跳入后续阶段.

在下面的示例中,如果`build_a``test_a`的速度比`build_b``test_b` ,则即使`build_b`仍在运行,GitLab 也会启动`deploy_a` .

使用 DAG build_a-> test_a-> deploy_a build_b-> test_b-> deploy_b 结束的图 LR 子图管道

匹配该图的示例 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 Pipelines[](#child--parent-pipelines "Permalink")

在上面的示例中,很明显,我们可以独立构建两种类型的东西. 这是通过[`trigger`关键字](../yaml/README.html#trigger)使用" [子/父管道"](../parent_child_pipelines.html)的理想情况. 它将配置分成多个文件,使事情变得非常简单. 您还可以将其与:

*   [`rules`关键字](../yaml/README.html#rules) :例如,仅在对该区域进行更改时才触发子管道.
*   [`include`关键字](../yaml/README.html#include) :引入常见行为,确保您不会重复自己.
*[管道](#directed-acyclic-graph-pipelines)内部的[DAG 管道](#directed-acyclic-graph-pipelines) ,实现了两者的好处.

graph LR 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` ,利用的 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." 
```

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

也可以将作业设置为在触发子管道之前或之后运行,例如,如果您具有通用的设置步骤或最后进行统一部署.