还有其他方法可以实现吗?最佳做法是什么?我不想维护比我需要的更多的构建定义。
有不同的方法来实现它,但不确定哪一种是最佳实践,这完全取决于您的要求或品味。
简单方法与你的想法类似。还需要创建一个新的构建管道。不同的是我们不需要维护这个构建定义。
详情:
- 在此管道中添加一个没有任何其他任务的新管道,并使用
path 过滤器以触发适当的构建(Api 客户端和
共享 Dto 项目)。
- 向您的原始 Azure Devops 构建管道添加 构建完成:
-
为基于Build.Reason 创建 2 个 Nuget 包的步骤添加 custom condition,例如:
and(succeeded(), eq(variables['Build.Reason'], 'BuildCompletion'))
现在,创建 2 个 Nuget 包的步骤仅在文件更改来自特定项目时执行。当然,这个解决方案的局限性在于,如果你已经有一个构建完成,它将无法工作。
如果上述方法不是您想要的,我们可以调用 REST API commits 来获取每个构建的提交信息:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}?changeCount=100&api-version=5.1
我们可以在返回的正文中找到更改/路径:
"changes": [
{
"item": {
"gitObjectType": "blob",
"path": "/.gitattributes",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/.gitattributes?versionType=Commit"
},
"changeType": "add"
},
{
"item": {
"gitObjectType": "blob",
"path": "/.gitignore",
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/.gitignore?versionType=Commit"
},
"changeType": "add"
},
{
"item": {
"gitObjectType": "tree",
"path": "/MyWebSite",
"isFolder": true,
"url": "https://dev.azure.com/fabrikam/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items/MyWebSite?versionType=Commit"
},
"changeType": "add"
},
然后我们可以使用 powershell 脚本遍历这些路径,看看它们是否包含 Api Client 和 Shared Dto 项目,如果是,我们设置一个带有值的变量,根据这个值为创建 2 的步骤添加条件Nuget 包。
注意:在使用 REST API commits 之前,我们需要使用 Commits - Get Commits 来获取最新的提交 ID:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?$top=1&api-version=5.1
希望这会有所帮助。