azure-devops - 运行两个或多个独立管道的 Azure 管道

标签 azure-devops azure-pipelines

是否可以在 Azure DevOps 中创建一个管道,它只运行两个或多个独立管道(并行或串行)而不做任何其他事情?

我有一个包含五个独立微服务的应用程序,每个微服务都可以通过其单独的构建管道 (YAML) 单独构建和部署。但是,将它们一起构建和部署通常对我们来说很方便;例如,将生成的人工制品标记为新版本的一部分时。我已经搜索了官方文档,但没有找到任何有关如何执行此操作的信息。 Pipeline triggers 不要帮助我,因为我需要保持这些管道分离,因为它们经常单独运行。我需要的是这样的东西(一厢情愿的伪代码):

trigger: none
pr: none

stages:
- stage: Pipeline 1
  jobs:
  - job: Pipeline 1
    displayName: 'Pipeline 1'
    pool:
      vmImage: 'ubuntu-16.04'
    steps:
    // custom script to run Pipeline 1
    - script: dotnet run pipeline1
      displayName: 'Run Pipeline 1 through a script'
    // or, alternatively, an in-built task to do it
    - task: RunPipeline@4
      displayName: 'Run Pipeline 1 through a task'
      inputs:
        PipelineName: 'Pipeline 1'
        etc.: ...

- stage: Pipeline 2
  jobs:
  - job: Pipeline 2
  ...

或者它可以是单个阶段中的单独作业,甚至是单个作业中的单独步骤 - 这都没有关系。我只想要一个包罗万象的“主管道”,只需按一下按钮,它就会启动所有单独的管道并构建所有服务。

最佳答案

Azure pipeline to run two or more independent pipelines

为此,我们可以使用 REST API Builds - Queue在主管道中排队其他构建管道:

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0

正如我测试的YAML文件:

pool:

  name: MyPrivateAgent


steps:

- task: PowerShell@2

  displayName: QueueBuildForPipelineA

  inputs:
    targetType : inline
    Script: |
     $body = '
     { 
             "definition": {
                 "id": DefinitionIdHere
             } 
     }'

     $bodyJson=$body | ConvertFrom-Json
     Write-Output $bodyJson
     $bodyString=$bodyJson | ConvertTo-Json -Depth 100
     Write-Output $bodyString
     $user="test"
     $token="PAT"
     $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

     $Uri = "https://dev.azure.com/YourOrganization/YourProjectName/_apis/build/builds?api-version=5.0"
     $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $bodyString -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

  condition: and(always(), eq(variables['TriggerPipelineA'], 'True'))


- task: PowerShell@2

  displayName: QueueBuildForPipelineB
  ...

此外,我还为任务添加了一个条件condition: and(always(), eq(variables['TriggerPipelineA'], 'True')),然后我在Variables中定义变量TriggerPipelineA我们可以通过在队列流水线期间将该变量的值覆盖为False来自由触发哪些流水线

enter image description here

希望这对您有所帮助。

关于azure-devops - 运行两个或多个独立管道的 Azure 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61506976/

相关文章:

git push --mirror 自动添加指向不正确工作项的链接

azure-devops - 在 Azure DevOps 拉取请求验证管道中以编程方式访问拉取请求更改?

visual-studio - Visual Studio Community Edition 无法访问 VSTS?

visual-studio - Visual Studio 2013 - 无法合并 View

azure - 如何创建Azure Devops的数组变量

azure - 是否可以将 azure 管道工件下载到管道内的本地文件夹?

azure-devops - VSTS 发布管理 : filter by branch on artifact source

azure-pipelines - 您可以在 Azure Pipeline 步骤中使用 pipenv 来安装依赖项吗?

azure-devops - 如何为 Nuget Artifact 配置 Azure Devops Pipeline 包名称

azure - 如何排除 Azure DevOps 构建管道中的一个分支