是否可以在 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来自由触发哪些流水线。
希望这对您有所帮助。
关于azure-devops - 运行两个或多个独立管道的 Azure 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61506976/