首页 > 解决方案 > 在 Azure Devops 中使用多存储库 CI 设置确定触发分支

问题描述

在 ADO 中,您可以根据本文档创建一个“存储库资源” 。“触发器”部分允许您为空间中的任何 Azure 存储库定义 CI 触发器。因此,给定以下配置:

回购:

resources:
  repositories:
    - repository: "Azure_Repo_1"
      type: git
      name: AzureRepo1
      ref: development
      trigger: 
        branches:
          include: 
            - development
            - staging
            - production

pool:
  vmImage: 'windows-latest'

jobs:
- template: Template.yml
  parameters:
    service: "development"
    run_tests: true

当我对 AzureRepo1 进行更改时,管道会触发。在运行时,我将如何确定目标存储库 (AzureRepo1) 的哪个分支(“生产”、“暂存”或“开发”)触发了构建?理想情况下,输入示例模板的“服务”参数将动态反映哪个分支触发了构建。

注意:“Build.SourceBranch”和“Build.SourceBranchName”似乎从托管 YML 文件的存储库(在本例中为 AzureRepo2)中提取了分支。

我错了。这些按预期运行。使用以下解决方案。

标签: azure-devopsazure-pipelines

解决方案


根据此处的文档:

当对其中一个存储库的更新触发管道时,会根据触发存储库设置以下变量:

  • 构建.存储库.ID
  • Build.Repository.Name
  • Build.Repository.Provider
  • Build.Repository.Uri
  • 构建.SourceBranch
  • Build.SourceBranchName
  • 构建.SourceVersion
  • Build.SourceVersionMessage

对于触发存储库,触发管道的提交决定了检出代码的版本。对于其他存储库,在 YAML 中为该存储库资源定义的 ref 确定了检出的默认版本。

如果在 AzureRepos1 上发生触发器,您应该在其中具有正确的分支名称Build.SourceBranchName

在此处输入图像描述


推荐阅读