
# 在IaC代码中定义流水线
IaC代码中的流水线可以由用户定义，用户可以根据自己的需求定义整个环境在变更时的执行过程，在变更执行过程中，系统只会变更被流水线引用的资源，本章介绍如何定义流水线。
#### 在哪里定义流水线
component间的编排在IaC Spec包中的meta.yaml文件中描述，涉及applyPipeline/pipelines两个字段。pipelines中支持定义多个流程，applyPipeline描述本次变更要使用的流程。pipeline能力丰富，通过设计pipeline可以实现精巧的多阶段部署、部分变更、主动暂停等复杂场景的变更编排。
图1meta.yaml   
![](https://support.huaweicloud.com/devg-appstage/zh-cn_image_0000001912695869.png)
#### 如何定义流水线
meta.yaml文件涉及applyPipeline/pipelines两个字段。pipelines中支持定义多个流程，applyPipeline描述本次变更要使用的流程。pipeline能力丰富，通过设计pipeline可以实现精巧的多阶段部署、部分变更、主动暂停等复杂场景的变更编排。
样例如下：
```
type: WiseCloud::Environment      # 保留字，声明这是一个针对环境的IAC代码
applyPipeline: default            # 代码中指定的默认pipeline，指定的pipeline必须是在pipelines中声明的
pipelines:                        # 列表，可以定义多个pipeline，并在执行任务时选择
  - name: default                 # pipeline名称
    action: Serial                # 此pipeline执行任务的策略 Serial（串行）/ Parallel（并行）
    tasks:                        # 声明流水线的子任务，通过声明pipeline的任务，实现对资源部署流程的编排
      - name: apply-chaosmonkey-stage1
        action: Serial
        tasks:
          - name: deploy
            action: Serial
            tasks:
              - name: apply-chaosmonkey-por
                action: Apply
                component:
                  name: WiseEyeChaosMonkeyPortal
  - name: all
    action: Serial
    tasks:
      - name: apply-chaosmonkey-stage1
        action: Serial
        tasks:
          - name: deploy
            action: Serial
            tasks:
              - name: apply-chaosmonkey-por
                action: Apply
                component:
                  name: WiseEyeChaosMonkeyPortal
      - name: apply-chaosmonkey-stage2
        action: Serial
        tasks:
          - name: deploy
            action: Serial
            tasks:
              - name: apply-chaosmonkey-mgr
                action: Apply
                component:
                  name: WiseEyeChaosMonkeyMgrService
```
表1meta.yaml字段说明 
| 字段            | 说明                                         |
|:---|:---|
| type          | 描述当前环境类型，当前为固定值WiseCloud::Environment。     |
| applyPipeline | 定义默认选用的组件编排流水线名称，当前默认使用environment-deploy。 |
| pipelines     | pipelines中支持定义多个流程。                        |
   
#### pipeline的任务声明
name/action都是一个流水线的基本信息，任务的编排是需要声明不同类型的task实现；当前支持的任务类型包括：Serial（串行）/Parallel（并行）/Apply（声明式任务执行），其中，Serial/Parallel不涉及资源部署，我们仅通过此类任务将需要部署的资源串联起来，从而推动环境变更的过程。如果把pipeline比作一棵树的声明，那么所有tasks都是这棵树的子节点，Apply是这棵树的叶子节点，只有叶子节点被执行时才会对环境产生影响。
图2pipeline任务声明   
![](https://support.huaweicloud.com/devg-appstage/zh-cn_image_0000001866637372.png)
- Serial（串行）/Parallel（并行）任务定义 定义Serial（串行）/Parallel（并行）任务将环境的资源变更流程串联起来。
  - Serial，串行执行，其参数tasks下的子任务会根据定义的顺序依次执行。
  
  - Parallel，并行执行，其参数tasks下的子任务会同时执行。
  
  
  ```
  - name: deploy    # 任务名称
    action: Serial  # 此任务下其子任务的执行策略 Serial（串行）/ Parallel（并行）
    tasks:          # 子任务，必填，子任务可以是任意类型的的任务；
      - name: apply-chaosmonkey-mgr
        action: Apply
        component:
          name: WiseEyeChaosMonkeyMgrService
  ```
  
- Apply（声明式任务执行）任务定义 我们可以通过定义Apply类型的任务声明一个环境的资源，这些资源可以是NuwaContainer，数据库，配置等；
  ```
  - name: apply-chaosmonkey-por      # 任务名称
    action: Apply                    # 任务类型
    inform:                          # 执行后通知配置
      message: finish stage2 notification
    confirm:                         # 执行前确认配置
      message: pause before execute task
    component:                       # 需要执行变更的component
      name: WiseEyeChaosMonkeyPortal # component名称
      resources:                     # component下需要执行变更的资源
        - name: por-nghbfcj1
          type: WiseCloud::MicroService::NuwaContainer #
          properties:
            grayStage:
              grayInstances: 100    # 灰度百分比
              grayStatus: 2         # 灰度实例对接SLB时的状态，1：生产，2：灰度
              grayProcess: FINISHED # 灰度状态，灰度处理完成
  ```
  
表2组件编排流水线（pipeline）对象定义 
| 字段名称   | 字段类型                 | 必选 | 描述                                                                                                                                                                                                                                                                                                                                |
|:---|:---|:---|:---|
| name   | String               | 是  | 流水线的名称，用户可自定义。                                                                                                                                                                                                                                                                                                                    |
| action | String               | 是  | 编排方式，可选值Serial/Parallel。 - Serial，串行执行，其参数tasks下的子任务会根据定义的顺序依次执行。  - Parallel，并行执行，其参数tasks下的子任务会同时执行。   |
| tasks  | List\<PipelineTask\> | 是  | 声明流水线的子任务，通过声明pipeline的任务，实现对资源部署流程的编排。                                                                                                                                                                                                                                                                                           |
   
表3PipelineTasks字段说明 
| 字段名称      | 字段类型                 | 必选 | 描述                              |
|:---|:---|:---|:---|
| name      | String               | 否  | 子任务的名称，用户可自定义。                  |
| action    | String               | 是  | 编排方式，可选值：Serial/Parallel/Apply。 |
| tasks     | List\<PipelineTask\> | 否  | 编排方式为Serial/Parallel时必选。        |
| component | ApplyComponent       | 否  | 需要执行变更的component，编排方式为Apply时必选。 |
   
任务的数据结构在设计上支持了无限层级嵌套，配合action属性可以实现任意的任务串并行编排。
表4component字段说明 
| 字段名称      | 必选 | 描述                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|
| name      | 是  | 组件名称，填写的值必须为已声明的组件，否则在校验阶段会报错。                                                                                                                                                                                                                                                                       |
| resources | 否  | 选择变更的资源，选填，该参数可用于部分部署。 如果不填写则会变更此component下所有资源，使用name+type+alias确认唯一资源（alias是资源的别名，如果component下不存在同名资源则不需要填写）。 如果填写component中不存在的资源，系统会在校验阶段报错；resources下可以定义properties，这样在执行此任务时，这些properties的值就会去覆盖你在resources.yaml下定义的资源参数。 |
   
