YAML Pipeline 能否检测云与自托管构建?

Can YAML Pipeline detect cloud vs Self-Hosted build?

是否可以在单个 YAML 管道中确定代理是 运行 自托管还是在云构建中?如果是这样,人们将如何确定这一点?

我需要我的管道在两个位置都工作,但某些步骤必须仅在我在一个或另一个上构建时发生。

我找不到直接的解决方案,但可以使用 conditions and agent variables

例如,我的代理池中有 Hosted Agent

并且在 YAML 中,我可以使用这个名称来调节 运行 步骤取决于它:

steps:
- script: dotnet build --configuration $(buildConfiguration)
  condition: eq(variables['Agent.Name'],'Hosted Agent') 
  displayName: 'dotnet build $(buildConfiguration)'

Is it possible to determine within a single YAML Pipeline whether the agent is running self-hosted or in a cloud build? If so, how would one go about determining that?

在 YAML 管道中,有 pool 关键字,用于管道作业的池。

因此,我们可以使用此 pool 关键字在单个 YAML 管道中确定代理是 运行 自托管还是在云构建中:

如果您使用自托管代理:

pool:
  name: string  # name of the pool to run this job in

如果您使用托管代理:

pool:
  vmImage: string # name of the VM image you want to use; valid only in the Microsoft-hosted pool

并查看 available virtual machine image 了解更多详情。

此外,如果我们不添加pool关键字,Azure pipeline将使用默认代理。您可以从 More action -> Triggers-> YAML of pipeline:

检查并更改它

I need my pipeline to work in both locations but certain steps must only occur if I'm building on one or the other

正如Bartosz回答的那样,我们可以使用Agent variables的条件来实现这个请求。