Azure YAML 管道:是否可以找出哪个管道触发了构建?

Azure YAML Pipelines: Is it possible to find out which pipeline triggered a build?

我的 Azure DevOps 项目有两个存储库。一个用于云基础设施部署,另一个包含我的应用程序代码。

我有一个 YAML 管道,它在任何这些回购构建管道完成后触发。管道看起来有点像这样:

resources:
  pipelines:
  - pipeline: MyProject-Code
  - pipeline: MyProject-Infrastructure
  
jobs:
- job: DeployInfrastructure
  steps:
  # Here are the tasks the deploy the project infrastructure

- job: DeployCode
  steps:
  # Here are the tasks that deploy the code

我想对 DeployInfrastructure 作业设置一个条件,以便它仅在触发管道是基础结构管道时才执行,因为如果更改仅影响应用程序代码,我不需要重新部署它。

但是,在阅读 documentation from Microsoft 时,似乎没有一种非常直接的方法可以做到这一点。

看看Pipeline resource variables

In each run, the metadata for a pipeline resource is available to all jobs in the form of predefined variables. The is the identifier that you gave for your pipeline resource. Pipeline resources variables are only available at runtime.

还有许多 predefined variables 称为 Build.TriggeredBy.*,其中 Build.TriggeredBy.DefinitionName,但文档建议对于带有管道触发器的 yaml 管道,应改用资源变量

If the build was triggered by another build, then this variable is set to the name of the triggering build pipeline. In Classic pipelines, this variable is triggered by a build completion trigger.

This variable is agent-scoped, and can be used as an environment variable in a script and as a parameter in a build task, but not as part of the build number or as a version control tag.

If you are triggering a YAML pipeline using resources, you should use the resources variables instead.