Azure管道的并行化

时间:2020-07-21 14:12:19

标签: azure azure-pipelines

我在YAML中有一个用于升级基础结构的管道(我有2个阶段,每个阶段包含一系列作业)

我现在想同时升级多个基础架构,即,将代表部署的标识符列表传递到管道,然后让其分别升级。

这里组织管道的最佳实践是什么?感觉我需要使用循环来生成一组并行作业吗?

据我所知,任何工作失败都会导致完全失败,这可能会使我们处于非常混乱的状态。

2 个答案:

答案 0 :(得分:0)

如果您为组织购买了parallel jobs。您可以使用${{each id in parameters.identities}},使用Template根据identities参数生成多个作业。

因此,您可以将将基础结构升级的工作转移到模板中,并按如下所示定义yaml管道。参见以下示例:

模板文件:upgrade-infrastructure.yml

parameters:
  id: 1
  
jobs:
- job: upgradeinfra${{parameters.id}}

  steps:
  - powershell: echo "upgrade-infra-${{parameters.id}}"

azure-pipelines.yml

#define the identities as a object to hold a array object of ids
parameters:
- name: identities 
  type: object
  default:
    ids:
    - 1
    - 2


trigger: none

stages:
- stage: Upgrage
  pool: 
    vmImage: windows-latest
  
  jobs:
  - job: A
    steps:
    - powershell: echo "job A"    
  
  #loop through the ids array object and the each id to the template paramter to generate multiple jobs for each id.
  #indentation is very important, bad indentation may cause pipeline compile error.
  - ${{ each id in parameters.identities.ids }}:
    - template: upgrade-infrastructure.yml
      parameters: 
        id: ${{id}}

按照上述步骤设置yaml管道后,可以在执行管道时在参数中输入标识:

enter image description here

然后您将看到多个作业已生成并并行运行:

enter image description here

答案 1 :(得分:0)

要使您的部署并行运行,您所需要做的就是设置依赖项。 (将自动设置对上一步的依赖性)。这是一个阶段的示例,该阶段仅取决于构建,然后所有阶段才能并行运行:

stage:
   -stages : DeployTo${{ parameters.environment }}
     dependsOn: ["Build"] //The stage that build the code is called "Build"
   

结果如下所示: enter image description here

没有DependsOn属性,您的管道阶段将按顺序运行,如下所示:

stages:
   -stage : DeployTo${{ parameters.environment }}

enter image description here

相关问题