如何在 Azure Pipeline 中启用 Git 子模块的检出?

7
在我们的Git项目中,我们在.gitmodules文件中定义了以下内容。
[submodule "DBPkg"]
  path = DBPkg
  url = https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg

在构建 Azure Pipeline 时,在我的“获取源”管道步骤中(我正在使用“Azure Repos Git”),我已启用子模块的检出。

enter image description here

在我们的项目设置中,我还进行了以下配置...

enter image description here

然而,在构建项目时,当尝试检出子模块时,它会因以下原因中止...
2020-10-16T20:29:44.9912145Z HEAD is now at e06abce Modified docker file to remove unnecessary container name.
2020-10-16T20:29:44.9913415Z ##[command]git submodule sync
2020-10-16T20:29:44.9920288Z ##[command]git -c http.https://MainCo@dev.azure.com.extraheader="AUTHORIZATION: bearer ***" submodule update --init --force
2020-10-16T20:29:44.9923206Z Submodule 'DBPkg' (https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg) registered for path 'DBPkg'
2020-10-16T20:29:44.9923842Z Cloning into '/home/vsts/work/1/s/DBPkg'...
2020-10-16T20:29:44.9924246Z remote: TF401019: The Git repository with name or identifier DBPkg does not exist or you do not have permissions for the operation you are attempting.
2020-10-16T20:29:44.9924850Z fatal: repository 'https://dev.azure.com/MainCo/OurProject/_git/DBPkg/' not found
2020-10-16T20:29:44.9925716Z fatal: clone of 'https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg' into submodule path '/home/vsts/work/1/s/DBPkg' failed
2020-10-16T20:29:44.9926288Z Failed to clone 'DBPkg'. Retry scheduled
2020-10-16T20:29:44.9926718Z Cloning into '/home/vsts/work/1/s/DBPkg'...
2020-10-16T20:29:44.9927132Z remote: TF401019: The Git repository with name or identifier DBPkg does not exist or you do not have permissions for the operation you are attempting.
2020-10-16T20:29:44.9928020Z fatal: repository 'https://dev.azure.com/MainCo/OurProject/_git/DBPkg/' not found
2020-10-16T20:29:44.9928801Z fatal: clone of 'https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg' into submodule path '/home/vsts/work/1/s/DBPkg' failed
2020-10-16T20:29:44.9929412Z Failed to clone 'DBPkg' a second time, aborting

我需要做什么来授权管道步骤检出子模块?.gitmodules中的URL直接从“存储库”部分剪切并粘贴。

编辑:从门户网站复制的有关存储库的URL如下:

https://MainCo@dev.azure.com/MainCo/OurProject/_git/MainAPIs
https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg

所以子模块位于 https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg,但不确定如何用 Git 子模块的术语来表述上述内容。
编辑 2:
针对所给答案,以下是代理报告的错误输出...
...
2020-10-20T13:21:23.9543308Z ##[command]git submodule sync
2020-10-20T13:21:23.9892501Z ##[command]git -c http.https://MainCo@dev.azure.com.extraheader="AUTHORIZATION: bearer ***" submodule update --init --force
2020-10-20T13:21:24.0147532Z Submodule 'DBPkg' (https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg) registered for path 'DBPkg'
2020-10-20T13:21:24.0194148Z Cloning into '/home/vsts/work/1/s/DBPkg'...
2020-10-20T13:21:24.1694357Z remote: TF401019: The Git repository with name or identifier DBPkg does not exist or you do not have permissions for the operation you are attempting.
2020-10-20T13:21:24.1696067Z fatal: repository 'https://dev.azure.com/MainCo/OurProject/_git/DBPkg/' not found
2020-10-20T13:21:24.1726598Z fatal: clone of 'https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg' into submodule path '/home/vsts/work/1/s/DBPkg' failed
2020-10-20T13:21:24.1731117Z Failed to clone 'DBPkg'. Retry scheduled
2020-10-20T13:21:24.1760958Z Cloning into '/home/vsts/work/1/s/DBPkg'...
2020-10-20T13:21:24.3885945Z remote: TF401019: The Git repository with name or identifier DBPkg does not exist or you do not have permissions for the operation you are attempting.
2020-10-20T13:21:24.3892068Z fatal: repository 'https://dev.azure.com/MainCo/OurProject/_git/DBPkg/' not found
2020-10-20T13:21:24.3902720Z fatal: clone of 'https://MainCo@dev.azure.com/MainCo/OurProject/_git/DBPkg' into submodule path '/home/vsts/work/1/s/DBPkg' failed
2020-10-20T13:21:24.3909036Z Failed to clone 'DBPkg' a second time, aborting
2020-10-20T13:21:24.3987155Z ##[error]Git submodule update failed with exit code: 

1


你知道路径 /home/vsts/work/1/s/DBPkg 是否存在吗?如果不存在,在其中创建 DBPkg 文件夹。 - Patrick
3个回答

4

更新2

流水线可以访问授权项目中的任何Azure DevOps存储库,如前一节“将作业授权范围限制为当前项目”所述,除非启用了“将作业授权范围限制为引用的Azure DevOps存储库”。

启用此选项后,您可以将所有流水线的访问范围缩小到仅限于由使用该存储库的流水线作业中的检出步骤明确引用的Azure DevOps存储库。

尝试关闭此选项并再次检查。

如果仍然无法工作,则需要检查存储库的安全性。 查找您的 [project name] 构建服务帐户和项目集合构建服务帐户,确保它们都具有足够的权限来访问您的两个 git 存储库。

在此输入图片描述


更新

尝试在 .gitmodules 中使用此命令,因为子模块存储库与父级位于同一 URL 中:

[submodule "DBPkg"]
  path = DBPkg
  url = ../DBPkg

您还可以查看此链接:https://dev59.com/b2sz5IYBdhLWcg3wsaLN#34618962


构建流程将检出您的Git子模块,只要它们是:

  • 未经身份验证:公共的、未经身份验证的存储库,克隆或提取不需要凭据。

  • 已经过身份验证:

    1. 包含在与上述Git repo相同的项目、GitHub组织或Bitbucket Cloud帐户中。

    2. 通过使用相对于主存储库的URL进行添加。例如,以下内容将被检出:git submodule add /../../submodule.git mymodule,而以下内容则不会被检出: git submodule add https://dev.azure.com/fabrikamfiber/_git/ConsoleApp mymodule

您还可以查看Azure DevOps文档这里

类似的博客供您参考:在(私人)Azure DevOps存储库中使用Git子模块


对于第二点,“git submodule add /../../submodule.git”,我的问题是“.gitmodules”文件需要长什么样子?我编辑了我的问题,包括了这些仓库的URL,除了最后一部分外都是相同的。 - Dave
我会盲目尝试 ../DBPkg 吗? - LeGEC
嗨@PatrickLu-MSFT,我尝试了你的建议,并将错误输出作为另一个编辑包含在我的问题中。我对这个“DBPkg不存在或者您没有权限执行您正在尝试的操作”的信息非常困扰。我是否应该在管道中设置检出权限的某个地方? - Dave
嗨,Dave,请检查我的更新回复。希望现在可以工作了。 - PatrickLu-MSFT
1
嗨,是的,取消选中“启用限制作业授权范围仅限于引用的 Azure DevOps 存储库”与您列出的新的 .gitmodules 格式相结合就可以解决问题了! - Dave

2
如果您没有更改“将作业授权范围限制为引用的Azure DevOps存储库”的权限,则添加子模块存储库的检出步骤可以让我授予其权限,然后子模块就可以正常工作了。不过这样会下载两次存储库。
- checkout: git://project/RepositoryReferencedByTheSubmodule
- checkout: self
  submodules: true

URL无需匹配,子模块可以指向类似于https://xxx@dev.azure.com/xxx/_git/RepositoryReferencedByTheSubmodule的内容。


只是想澄清一些对我来说不太明显的事情 - git 链接是一种特殊格式,用于指向 DevOps 资源:git://YOUR_PROJECT_NAME/THE_SUBMODULE_REPO - 01F0
它抱怨无法检出一些分支refs/heads/master,而我有不同的分支名称:main,这不是默认值。将其设置为默认值后,就可以正常工作了。否则,您可能需要使用此语法:git://project/RepositoryReferencedByTheSubmodule@refs/heads/main更多示例:检出特定的ref - Janis Veinbergs

0

我正准备为我的子模块添加一个显式的检出(如@Fábio Machado de Oliveira所建议的那样),但我又仔细看了一下项目管道选项。

project pipeline options

我的子模块与引用它们的存储库在同一个项目中,因此第三个和第四个复选框不会限制访问(它们限制到项目级别范围,这对我来说很好)。

然而,最后一个复选框“保护 YAML 管道中的存储库访问”,它“生成一个作业访问令牌,该令牌的范围仅限于在 YAML 管道中明确引用的存储库”,是阻止访问同一项目中隐式引用的存储库的原因

虽然这可能是默认情况下要有的好事,但对于子模块来说,这意味着我必须在所有具有它们的管道中显式引用所有子模块存储库。在我的情况下,子模块定义在同一个项目中,因此项目范围的令牌足够安全。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接