根据对子文件夹的更改触发 Azure DevOps 构建 [英] Triggering Azure DevOps builds based on changes to sub folders

查看:19
本文介绍了根据对子文件夹的更改触发 Azure DevOps 构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个项目的 Visual Studio 解决方案,每个项目都是一个单独的微服务.把所有的服务都放在同一个解决方案中对开发团队来说非常方便,而且git repo,服务之间可以互相调用.

I have a Visual Studio solution with multiple projects, each project is an individual microservice. It is very convenient to the development team to have all the services in the same solution, and git repo, as services can call each other.

Master.sln - SubFolderA - MicroserviceA.sln
           - SubFolderB - MicroserviceB.sln
           - SubFolderC - MicroserviceC.sln

但是,我希望在 Azure DevOps 中的单个微服务发生变化时独立构建/发布它们,因此如果 ServiceA 是唯一要更改的服务,那么 ServiceA 是唯一构建和部署的服务.

I would, however, like to independently build/release the individual microservices in Azure DevOps when they change, so if ServiceA is the only service to change, then ServiceA is the only service built and deployed.

为此,我创建了一个新的构建管道定义,其中路径过滤器"设置为在微服务文件夹的内容发生更改时触发构建(因此每个微服务添加一个路径过滤器以进行监控).

To this end I created a new build pipeline definition with "Path filters" set to trigger the build when the contents of a microservice folder change (so one path filter added per microservice to monitor).

我的问题是,当触发构建时(例如基于对 SubFolderA 的更改),我无法告诉构建定义仅在 SubFolderA 中构建 .sln 文件.

My problem here is that when a build is triggered (based on a change to SubFolderA e.g.) I have no way to tell the build definition to only build the .sln file in SubFolderA.

我可以为每个微服务创建一个单独的构建定义,并在单独的子文件夹上触发每个构建,但这会带来很大的开销,即我需要维护 15 个单独的构建定义(对于我构建的每个分支也是如此),并且我们的自托管构建代理所需的存储现在是 NumberOfService x NumberOfBranchesBeingBuild x SizeOfRepo.

I could create a separate build definition for each microservice and trigger each build on separate subfolders, but this would come at significant overhead, i.e. I would need to maintain 15 separate build definitions (same again for each branch I build), and the storage required on our self host build agent would now be NumberOfService x NumberOfBranchesBeingBuild x SizeOfRepo.

有没有办法使用带有 git路径过滤器"和多个定义的路径的单个构建定义,这反过来启动多个构建实例并将触发构建的路径的值提供给构建定义等等.sln 文件要构建的构建实例?

Is there a way to use a single Build Definition with git "Path filters" and multiple paths defined, which in turn kicks off multiple build instances and feeds the value of the path that triggered the build into the build definition and so telling the build instance which .sln file to build?

我希望这是有道理的!

推荐答案

你可以像下面这样

  1. 根据您的微服务创建变量,值为False"

例如,MicroserviceAUpdated="False",MicroserviceBUpdated="False" 等,

E.g,MicroserviceAUpdated= "False",MicroserviceBUpdated= "False" etc.,

  1. 在构建定义的开头添加 Powershell 脚本任务.powershell 脚本将执行以下操作:

获取构建中的变更集/提交以检查哪些文件发生了更改.

Get the changeset/commit in the build to check which files are changed.

  • MicroserviceAUpdated 变量更新为true"(如果有)SubFolderA 下的文件被更改.
  • MicroserviceBUpdated 变量更新为true"(如果有)
    SubFolderA 下的文件被更改.
  • Update the MicroserviceAUpdated variable to "true" if only any files are changed under SubFolderA.
  • Update the MicroserviceBUpdated variable to "true" if only any
    files are changed under SubFolderA.

等等......

  1. 为每个微服务创建单独的构建任务,配置构建任务以使用如下自定义条件运行

对于微服务A构建任务

自定义条件":and(succeeded(), eq(variables['MicroserviceAUpdated'], 'True'))

对于微服务B构建任务

自定义条件":and(succeeded(), eq(variables['MicroserviceBUpdated'], 'True'))

等等...

这样如果变量的值为False

对于第 2 步

$files=$(git diff HEAD HEAD~ --name-only)
$temp=$files -split ' '
$count=$temp.Length
echo "Total changed $count files"
For ($i=0; $i -lt $temp.Length; $i++)
{
  $name=$temp[$i]
  echo "this is $name file"
  if ($name -like "SubFolderA/*")
    {
      Write-Host "##vso[task.setvariable variable=MicroserviceAUpdated]True"
    }
}

这篇关于根据对子文件夹的更改触发 Azure DevOps 构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆