通过


查看管道中的多个存储库

Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022

Pipelines通常依赖于包含生成代码所需的源代码、工具、脚本或其他项的多个存储库。 通过在管道中使用多个 checkout 步骤,可以将其他存储库提取并签出到用于存储 YAML 管道之外的存储库。

指定多个存储库

可以将存储库指定为 存储库资源,也可以与 checkout 步骤内联。

支持以下存储库类型。


Azure Repos Gitgit

  • Azure DevOps Server (仅限于同一组织中的存储库)
  • Azure DevOps Services

GitHubgithub

  • Azure DevOps Services

GitHubEnterprisegithubenterprise

  • Azure DevOps Services

Bitbucket 云 (bitbucket)

  • Azure DevOps Services

重要

Azure DevOps Server 中的多存储库仅支持 Azure Repos Gitgit)存储库。

注意

Azure Pipelines为 Azure Repos Git 存储库提供 Limit 作业范围设置。 若要签出另一个project中托管的 Azure Repos Git 存储库,必须将 Limit 作业范围配置为允许access。 有关详细信息,请参阅 Limit 作业授权范围

支持以下 checkout 步骤组合。


checkout 步骤

默认行为就好像 checkout: self 是第一步,并且当前存储库已签出。


一个 checkout: none 步骤

不会同步或签出任何存储库。


一个 checkout: self 步骤

当前存储库已签出。


一个不是 checkoutselfnone 步骤

指定的存储库已签出,而不是 self


多个 checkout 步骤

除非在 path 步骤中指定了其他 checkout,否则每个指定的存储库都会签出到以该存储库命名的文件夹。 要签出 self 作为存储库之一,请使用 checkout: self 作为 checkout 步骤之一。


注意

签出包含管道的 Git 存储库以外的Azure Repos时,系统可能会提示你在首次运行管道之前授权access该资源。 有关详细信息,请参阅常见问题解答部分中的当我首次尝试签出一个不同的存储库时,为何系统提示我对资源进行授权?

存储库资源定义

如果存储库类型需要服务连接或其他扩展资源字段,则必须使用 存储库资源。 以下存储库类型需要服务连接。

存储库类型 服务连接
Bitbucket 云 Bitbucket 云
GitHub GitHub
GitHub Enterprise Server GitHub Enterprise Server
Azure Repos与管道不同的组织中的 Git 存储库 Azure Repos/Team Foundation Server

即使存储库类型不需要服务连接,也可以使用存储库资源,例如,如果已为不同存储库中的模板定义了存储库资源。

在下面的示例中,三个存储库声明为存储库资源。 另一个组织中的 Azure Repos Git 存储库GitHubBitbucket Cloud 存储库资源需要服务连接,这些资源被指定为这些存储库资源的 endpoint。 此示例包含四个 checkout 步骤,用于签出声明为存储库资源的三个存储库以及包含管道 YAML 的当前 self 存储库。

resources:
  repositories:
  - repository: MyGitHubRepo # The name used to reference this repository in the checkout step
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
  - repository: MyBitbucketRepo
    type: bitbucket
    endpoint: MyBitbucketServiceConnection
    name: MyBitbucketOrgOrUser/MyBitbucketRepo
  - repository: MyAzureReposGitRepository # In a different organization
    endpoint: MyAzureReposGitServiceConnection
    type: git
    name: OtherProject/MyAzureReposGitRepo

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- checkout: self
- checkout: MyGitHubRepo
- checkout: MyBitbucketRepo
- checkout: MyAzureReposGitRepository

- script: dir $(Build.SourcesDirectory)

如果 self 存储库名为 CurrentReposcript 命令将生成以下输出:CurrentRepo MyAzureReposGitRepo MyBitbucketRepo MyGitHubRepo。 在本示例中,由于签出步骤中未指定 name,因此对文件夹使用存储库名称(由存储库资源中的 path 属性指定)。 有关存储库文件夹名称和位置的详细信息,请参阅下面的签出路径部分。

内联语法签出

如果存储库不需要服务连接,则可按照 checkout 步骤声明它。

注意

只有同一组织中的Azure Repos Git 存储库才能使用内联语法。 Azure Repos其他组织中的 Git 存储库和其他受支持的存储库类型需要 service connection,并且必须声明为 repository 资源

steps:
- checkout: self
- checkout: git://MyProject/MyRepo # Azure Repos Git repository in the same organization

注意

在前面的示例中,指定了 self 签出存储库,以便签出与管道关联的存储库的源。

如果使用默认Azure Repos Git 存储库(其名称与 project 相同),请使用格式 - checkout: git://MyProject/MyRepo

签出路径

除非在 path 步骤中指定了 checkout,否则源代码将放置在默认目录中。 此目录会有所不同,具体取决于你签出的是一个存储库还是多个存储库。

  • 一个存储库:如果作业中只有一个 步骤,或者没有等效于 checkout 的签出步骤,则源代码将签出到名为 checkout: self 的目录中,该目录作为 s 的子文件夹存在。 如果 $(Agent.BuildDirectory)C:\agent\_work\1,代码将签出到 C:\agent\_work\1\s

  • 多个存储库:如果作业中有多个 步骤,源代码将作为 checkouts 的子文件夹签出到以存储库命名的目录中。 如果 $(Agent.BuildDirectory)C:\agent\_work\1 并且存储库命名为 toolscode,则代码将签出到 C:\agent\_work\1\s\toolsC:\agent\_work\1\s\code

    注意

    如果在 path 步骤中未指定 checkout,则将存储库的名称(而不是用于在 repository 步骤中引用存储库的 checkout 值)用于文件夹。

如果为 path 步骤指定了 checkout,则使用该路径(相对于 $(Agent.BuildDirectory))。

注意

如果使用默认路径,添加第二个存储库 checkout 步骤将更改第一个存储库的代码的默认路径。 例如,如果 tools 是唯一的存储库,名为 C:\agent\_work\1\s 的存储库的代码将签出到 tools,但如果添加了第二个存储库,tools 将签出到 C:\agent\_work\1\s\tools。 如果有任何步骤依赖于原始位置中的源代码,则必须更新这些步骤。

工作区存储库

在管道中使用多个 checkout 步骤(以及每个步骤的不同路径)时,可能需要将一个存储库的根目录用作默认工作目录。 如果是,可以将 workspaceRepo 输入设置为相关 true 步骤的 checkout

- checkout: git://project/first
  path: repo/first-repo

- checkout: git://project/second
  path: repo/second-repo
  workspaceRepo: true

- pwsh: pwd
# Expected output: $(Pipeline.Workspace)/repo/second-repo

签出特定引用

除非指定特定的引用,否则将签出默认分支。

如果使用内联语法,请通过追加 @<ref> 来指定引用。 例如:

- checkout: git://MyProject/MyRepo@features/tools # checks out the features/tools branch
- checkout: git://MyProject/MyRepo@refs/heads/features/tools # also checks out the features/tools branch
- checkout: git://MyProject/MyRepo@refs/tags/MyTag # checks out the commit referenced by MyTag.

重要

该值 checkout (包括内联 @<ref> 部分)在编译时解析为资源引用。 宏语法变量 ($(var)) 不会在 值中 checkout 展开。 例如, checkout: git://MyProject/MyRepo@$(branch) 不解析变量 — 管道尝试签出按字面命名 $(branch)的 ref,这通常失败并出现“找不到 ref”错误。

若要参数化分支,请使用 模板表达式${{ }})和 运行时参数,如以下示例所示。 ${{ variables.myVar }} 仅适用于在 YAML 中定义为文本值的变量(在编译时已知),不适用于队列时间或动态设置变量。

使用内联语法参数化分支

定义 运行时参数 ,并在内联签出值中使用模板表达式引用它:

parameters:
  - name: branch
    type: string
    default: 'main'

steps:
  - checkout: git://MyProject/MyRepo@${{ parameters.branch }}

使用存储库资源参数化分支

ref使用存储库资源上的属性从参数设置分支:

parameters:
  - name: branch
    type: string
    default: 'main'

resources:
  repositories:
    - repository: MyRepo
      type: git
      name: MyProject/MyRepo
      ref: ${{ parameters.branch }}

steps:
  - checkout: MyRepo

使用存储库资源时,请使用 ref 属性指定引用。 以下示例签出指定存储库的 features/tools/ 分支。

resources:
  repositories:
  - repository: MyGitHubRepo
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
    ref: features/tools

steps:
- checkout: MyGitHubRepo

以下示例使用 tags 签出由 MyTag 引用的提交。

resources:
  repositories:
  - repository: MyGitHubRepo
    type: github
    endpoint: MyGitHubServiceConnection
    name: MyGitHubOrgOrUser/MyGitHubRepo
    ref: refs/tags/MyTag

steps:
- checkout: MyGitHubRepo

触发器

在将更新推送到 self 存储库或声明为资源的任何存储库时,可以触发管道。 这在以下场景中很有用:

  • 你使用不同存储库中的工具或库。 每当更新工具或库时,你都希望为应用程序运行测试。
  • 你将 YAML 文件保存在与应用程序代码不同的存储库中。 每次将更新推送到应用程序存储库时,你都希望触发管道。

重要

存储库资源触发器仅适用于同一组织中的 Azure Repos Git 存储库,当 self 存储库类型Azure Repos Git 时。 它们不适用于GitHub或 Bitbucket 存储库资源。

batch 存储库资源触发器不支持。

如果未在存储库资源中指定 trigger 部分,则对该存储库的更改不会触发管道。 如果指定了 trigger 部分,则触发行为类似于 CI 触发器在自存储库中的工作方式。

如果为多个存储库资源指定了一个 trigger 部分,则对其中任一资源的更改将启动新运行。

触发管道时,Azure Pipelines必须确定应使用的 YAML 文件版本,以及应签出的每个存储库的版本。如果对 self 存储库进行更改会触发管道,则触发管道的提交用于确定 YAML 文件的版本。 如果对任何其他存储库资源的更改触发了管道,则使用 存储库的默认分支的最新版本的 YAML。

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

  • Build.Repository.ID
  • Build.Repository.Name
  • Build.Repository.Provider
  • Build.Repository.Uri
  • Build.SourceBranch
  • Build.SourceBranchName
  • Build.SourceVersion
  • Build.SourceVersionMessage

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

请考虑以下示例,其中 self 存储库包含 YAML 文件,存储库 AB 包含其他源代码。

trigger:
- main
- feature

resources:
  repositories:
  - repository: A
    type: git
    name: MyProject/A
    ref: main
    trigger:
    - main

  - repository: B
    type: git
    name: MyProject/B
    ref: release
    trigger:
    - main
    - release
steps:
- checkout: self
- checkout: A
- checkout: B

下表显示了使用上述 YAML 文件的管道为每个存储库签出的版本。

对下列项目所做的更改 是否管道触发 YAML 的版本 版本self 版本A 版本B
main 中的 self 从触发管道的 main 提交 从触发管道的 main 提交 最新,来自 main 最新,来自 release
feature 中的 self 从触发管道的 feature 提交 从触发管道的 feature 提交 最新,来自 main 最新,来自 release
main 中的 A 最新,来自 main 最新,来自 main 从触发管道的 main 提交 最新,来自 release
main 中的 B 最新,来自 main 最新,来自 main 最新,来自 main 从触发管道的 main 提交
release 中的 B 最新,来自 main 最新,来自 main 最新,来自 main 从触发管道的 release 提交

还可以在任何存储库中创建或更新拉取请求时触发管道。 To do此作,如上述示例一样声明 YAML 文件中的存储库资源,并在存储库中配置分支策略(仅Azure Repos)。

存储库详细信息

签出多个存储库时,有关 self 存储库的一些详细信息以变量的形式提供。 使用多存储库触发器时,其中一些变量包含有关触发存储库的信息。 有关作业使用的所有存储库的详细信息可作为称为 resources.repositories提供。

例如,若要获取非 self 存储库的引用,可以按照如下命令编写管道:

resources:
  repositories:
  - repository: other
    type: git
    name: MyProject/OtherTools

variables:
  tools.ref: $[ resources.repositories['other'].ref ]

steps:
- checkout: self
- checkout: other
- bash: |
    echo "Tools version: $TOOLS_REF"

FAQ

为什么无法从另一个project签出存储库? 这在过去可以。

Azure Pipelines为当前project设置提供一个 Limit 作业授权范围,启用时不允许管道在包含管道的project之外access资源。 可以在组织级别或project级别设置此设置。 如果启用此设置,则无法签出另一个project中的存储库,除非显式授予access。 有关详细信息,请参阅 Job 授权范围

当我首次尝试签出一个不同的存储库时,为何系统提示我对资源进行授权?

签出包含管道的 Git 存储库以外的Azure Repos时,系统可能会提示你在首次运行管道之前授权access该资源。 这些提示显示在管道运行摘要页上。

此管道需要权限才能access资源

对资源进行授权

选择“查看”或“对资源进行授权”,然后按照提示对资源进行授权。

正在等待审阅

Permit access

有关详细信息,请参阅 YAML 管道的授权故障排除