如何获取有关 Azure Pipelines 运行 中资源的信息?

How to get information about resources in an Azure Pipelines run?

我在一个平台团队工作,该团队构建和支持 Azure Pipelines YAML 模板以及这些管道的各种自定义脚本。我组织中的人员使用这些 YAML 模板,例如:

resources:
  repositories:
    - repository: templates
      type: git
      name: PROJECT/cicd_stuff
      ref: refs/heads/releases/v0.21
extends:
  template: our_awesome_template.yml@templates
  parameters:
    ...

为了可靠地将我们的脚本版本与 YAML 模板的版本相匹配,我们现在在我们的管道模板中包含一个额外的构建阶段,用于检查 repo 模板 repo 并将我们所有的脚本放在一个工件中以用于 运行。使用“发布分支”使我们能够安全地发布和测试新功能和对我们管道的修复:团队可以按照自己的节奏升级他们的管道,旧版本在一段时间内仍然受到支持。

我想开始将 Azure Artifacts 用于我们的脚本工件,但现在我想知道“我如何才能确定我应该从 Azure Artifacts 下载哪个版本的脚本?”资源块中包含的信息对我有用,但我似乎无法使用表达式或预定义变量访问它。我目前能想到的唯一解决方案是使用 az pipelines cli。有什么想法吗?

how can I determine which version of my scripts I should be downloading from Azure Artifacts?

如果功能版本是目标版本,您可以尝试以下yaml来获取它的值。参见:Repository details 了解详情。

resources:
  repositories:
    - repository: templates
      type: git
      name: PROJECT/cicd_stuff
      ref: releases/v0.21

variables:
  tools.ref: $[ resources.repositories['templates'].ref ]
  
pool:
  vmImage: ubuntu-latest

steps:
- bash: |
    echo "Tools version: $TOOLS_REF"

您也可以参考此文档使用标签ref: refs/tags/v1.0 # optional ref to pin to 尝试 Daniel 的解决方案:Use other repositories and Checking out a specific ref.

更新>>当前没有关于用于包含存储库资源的名称的可用预定义变量(在本例中为 'templates')。如果我们知道存储库别名,则可以通过 $[ resources.repositories['templates'].name].

解析存储库名称

另一个发现是存储库资源将作为构建工件添加,我们可以从 API:GET https://dev.azure.com/{organization}/{project}/_build/results?buildId={buildId}&view=artifacts&__rt=fps&type=consumedArtifacts(我从浏览器开发工具中获取这个 API ).而 buildId 可以使用变量 Build.BuildId 得到。有关详细信息,请参阅:Build variables (DevOps Services)。从响应中搜索 consumedSources 字段以找到下面的 json 段,它将 return 所有工件,您可以找到存储库资源及其所有详细信息。